当前位置:编程学习 > wap >>

求助关于Android中的SQLException

求教高手,加下划线处为什么报错
package yin.yun.contentProvider;

import java.sql.SQLException;



import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
//import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;

public class PeopleProvider extends ContentProvider {

    private static final String DB_NAME="people.db";
    private static final String DB_TABLE="peopleinfo";
    private static final int DB_VERSION=1;

    private SQLiteDatabase db;
    private DBOpenHelper dbOpenHelper;
    private static final int MULTIPLE_PEOPLE=1;
    private static final int SINGLE_PEOPLE=2;
    private static final UriMatcher uriMatcher;
    static{
     uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
     uriMatcher.addURI(People.AUTHORITY,People.PATH_MULTIPLE,MULTIPLE_PEOPLE);
     uriMatcher.addURI(People.AUTHORITY, People.PATH_SINGLE,SINGLE_PEOPLE);
    
    }


@Override
public String getType(Uri uri) {
switch(uriMatcher.match(uri)){
case MULTIPLE_PEOPLE:
return People.MINE_TYPE_MULTIPLE;
case SINGLE_PEOPLE:
    return People.MINE_TYPE_SINGLE;
    default:
     throw new IllegalArgumentException("Unkown uri:"+uri);
        
}

}
public Uri insert(Uri uri,ContentValues values){


long id=db.insert(DB_TABLE,null,values);
if(id>0){

Uri newUri=ContentUris.withAppendedId(People.CONTENT_URI, id);
getContext().getContentResolver().notifyChange(newUri, null);
return newUri;
}
求教高手,这里为什么报错?
throw new SQLException("Failed to insert row into" + uri);
        
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count=0;
switch(uriMatcher.match(uri)){
case MULTIPLE_PEOPLE:
count=db.delete(DB_TABLE,selection,selectionArgs);
break;
case SINGLE_PEOPLE:
String segment=uri.getPathSegments().get(1);
count=db.delete(DB_TABLE, People.KEY_ID+"="+segment, selectionArgs);
break;
default:
throw new IllegalArgumentException("Unsupported URI:"+uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}





@Override
public boolean onCreate() {
Context context=getContext();
dbOpenHelper=new DBOpenHelper(context, DB_NAME, null, DB_VERSION);
db=dbOpenHelper.getWritableDatabase();
if(db==null)
return false;
else
return true;

}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {

SQLiteQueryBuilder qb=new SQLiteQueryBuilder();
qb.setTables(DB_TABLE);
switch(uriMatcher.match(uri)){
case SINGLE_PEOPLE:
qb.appendWhere(People.KEY_ID+"="+uri.getPathSegments().get(1));
break;
default:
break;

}
Cursor cursor=qb.query(db, projection,selection, selectionArgs, null,null,sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count;
switch(uriMatcher.match(uri)){
case MULTIPLE_PEOPLE:
count=db.update(DB_TABLE, values, selection, selectionArgs);
case SINGLE_PEOPLE:
String segment=uri.getPathSegments().get(1);
count=db.update(DB_TABLE, values,People.KEY_ID+"="+segment,selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknow URI:"+uri);
}
getContext().getContentResolver().notifyChange(uri,null);

return count;
}
private static class DBOpenHelper extends SQLiteOpenHelper{

public DBOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub 
}
private static final String DB_CREATE="create table"+DB_TABLE+"("+People.KEY_ID+"integer primary key autoincrement," +
""+People.KEY_NAME+"text not null ,"+People.KEY_AGE+"integer,"+People.KEY_HEIGHT+"float);";
@Override
    public void onCreate(SQLiteDatabase _db){
_db.execSQL(DB_CREATE);

}
@Override
public void onUpgrade(SQLiteDatabase _db,int _oldVersion,int _newVersion){

_db.execSQL("DROP TABLE IF EXISTS"+DB_TABLE);
onCreate(_db);

}
}
}
--------------------编程问答-------------------- 插入数据失败了,你把生成的SQL语句翻出来看看就知道了。 --------------------编程问答-------------------- 一样的错,求解 --------------------编程问答-------------------- import java.sql.SQLException;
这一句 --------------------编程问答--------------------
引用 楼主 YinYunJie 的回复:
求教高手,加下划线处为什么报错
package yin.yun.contentProvider;

import java.sql.SQLException;



import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentVa……


public Uri insert(Uri uri,ContentValues values){


long id=db.insert(DB_TABLE,null,values);
if(id>0){

Uri newUri=ContentUris.withAppendedId(People.CONTENT_URI, id);
getContext().getContentResolver().notifyChange(newUri, null);
return newUri;
}
求教高手,这里为什么报错?
throw new SQLException("Failed to insert row into" + uri);
        
}

这个方法你都return 了  还抛什么异常啊~     你要抛异常  请添加在return 前 --------------------编程问答--------------------
引用 4 楼 ConnectionUtils 的回复:
引用 楼主 YinYunJie 的回复:求教高手,加下划线处为什么报错
package yin.yun.contentProvider;

import java.sql.SQLException;



import android.content.ContentProvider;
import android.content.ContentUris;
……


前面有if的 --------------------编程问答--------------------
引用 楼主 YinYunJie 的回复:
求教高手,加下划线处为什么报错
package yin.yun.contentProvider;

import java.sql.SQLException;



import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentVa……


求教高手,这里为什么报错?
throw new SQLException("Failed to insert row into" + uri);


这句话  后面加个return null;   
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,