我试图更新我的数据库,但每次我收到错误说没有这样的列。我还是Android的初学者,所以任何帮助都会受到赞赏。
提前感谢你。
这是我的2个数据库类:
AreaMonitoringDB:
public class AreaMonitoringDB extends SQLiteOpenHelper {
private static final String DEBUG_TAG = "AreaMonitoringDB";
private static final String DATABASE_NAME = "AreaMonitoring.db";
private static final int DATABASE_VERSION = 10;
public static final String TABLE_NAME = "AreaMonitor";
public static final String ID = "_id";
public static final String COL_FLAG= "enable1";
public static final String COL_FLAG2 = "enable2";
public static final String COL_FLAG3 = "enable3";
public static final String COL_NUMBER = "tel_number";
private static final String CREATE_TABLE_AREAMONITOR = "create table "
+ TABLE_NAME + " ("
+ ID + " integer primary key, "
+ COL_FLAG + " integer not null, "
+ COL_FLAG2 + " integer not null, "
+ COL_FLAG3 + " integer not null, "
+ COL_NUMBER + " text );";
private static final String DB_SCHEMA = CREATE_TABLE_AREAMONITOR;
public AreaMonitoringDB(Context cx){
super(cx, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_SCHEMA);
//init(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DEBUG_TAG, "Upgrading database. Existing contents will be lost. ["
+ oldVersion + "]->[" + newVersion + "]");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
AreaMonitoringProvider:
public class AreaMonitoringProvider extends ContentProvider {
private static final String TAG = "AreaMonitoringProvider";
private AreaMonitoringDB mDB;
private static final String AUTHORITY = "com.data.AreaMonitoringProvider";
private static final String BASE_PATH = "AreaMonitor";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + BASE_PATH);
private static final UriMatcher sURIMatcher = new UriMatcher(
UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, BASE_PATH, 0);
}
@Override
public boolean onCreate() {
mDB = new AreaMonitoringDB(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(AreaMonitoringDB.TABLE_NAME);
Cursor cursor = queryBuilder.query(mDB.getReadableDatabase(),
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) {
SQLiteDatabase sqlDB = mDB.getWritableDatabase();
int rowsAffected;
String id = uri.getLastPathSegment();
StringBuilder modSelection = new StringBuilder(AreaMonitoringDB.ID
+ "=" + id);
if (!TextUtils.isEmpty(selection)) {
modSelection.append(" AND " + selection);
}
return rowsAffected = sqlDB.update(AreaMonitoringDB.TABLE_NAME,
values, modSelection.toString(), null);
}
@Override
public int delete(Uri arg0, String arg1, String[] arg2) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri arg0, ContentValues arg1) {
// TODO Auto-generated method stub
return null;
}
}
在我的主要活动中,我使用以下方式更新它:
ContentResolver cr1 = getContentResolver();
ContentValues values = new ContentValues();
values.put(AreaMonitoringDB.COL_FLAG, flag);
values.put(AreaMonitoringDB.COL_NUMBER, stringArea);
Toast.makeText(getApplicationContext(), " "+flag+" "+stringArea, Toast.LENGTH_SHORT).show();
cr1.update(AreaMonitoringProvider.CONTENT_URI,
values,
AreaMonitoringDB.ID + "="+ 0,
null);
Log.e("TEST PROVIDER"," NUMBER "+ AreaMonitoringDB.COL_NUMBER);
Log.e("TEST PROVIDER"," ID "+ AreaMonitoringDB.ID );
logcat的:
E/AndroidRuntime( 3831): FATAL EXCEPTION: main
E/AndroidRuntime( 3831): android.database.sqlite.SQLiteException: no such column
: AreaMonitor: , while compiling: UPDATE AreaMonitor SET enable1=?,tel_number=?
WHERE _id=AreaMonitor AND _id=0
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteCompiledSql.nat
ive_compile(Native Method)
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteCompiledSql.<in
it>(SQLiteCompiledSql.java:64)
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteProgram.compile
Sql(SQLiteProgram.java:146)
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteProgram.compile
AndbindAllArgs(SQLiteProgram.java:367)
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteStatement.acqui
reAndLock(SQLiteStatement.java:253)
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteStatement.execu
teUpdateDelete(SQLiteStatement.java:83)
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteDatabase.update
WithOnConflict(SQLiteDatabase.java:1829)
E/AndroidRuntime( 3831): at android.database.sqlite.SQLiteDatabase.update
(SQLiteDatabase.java:1780)
E/AndroidRuntime( 3831): at com.notfall.AreaMonitoringProvider.update(Are
aMonitoringProvider.java:73)
E/AndroidRuntime( 3831): at android.content.ContentProvider$Transport.upd
ate(ContentProvider.java:233)
E/AndroidRuntime( 3831): at android.content.ContentResolver.update(Conten
tResolver.java:847)
E/AndroidRuntime( 3831): at com.notfall.MainActivity$1$1.onClick(MainActi
vity.java:221)
E/AndroidRuntime( 3831): at com.android.internal.app.AlertController$Butt
onHandler.handleMessage(AlertController.java:166)
E/AndroidRuntime( 3831): at android.os.Handler.dispatchMessage(Handler.ja
va:99)
E/AndroidRuntime( 3831): at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime( 3831): at android.app.ActivityThread.main(ActivityThrea
d.java:4025)
E/AndroidRuntime( 3831): at java.lang.reflect.Method.invokeNative(Native
Method)
E/AndroidRuntime( 3831): at java.lang.reflect.Method.invoke(Method.java:4
91)
E/AndroidRuntime( 3831): at com.android.internal.os.ZygoteInit$MethodAndA
rgsCaller.run(ZygoteInit.java:841)
E/AndroidRuntime( 3831): at com.android.internal.os.ZygoteInit.main(Zygot
eInit.java:599)
E/AndroidRuntime( 3831): at dalvik.system.NativeStart.main(Native Method)
@SLUKIAN 您可以查看此代码中的CursorIndexOutOdBounds Exception
ContentResolver cr = getContentResolver();
final String[] projection = { AreaMonitoringDB.ID, AreaMonitoringDB.COL_FLAG,AreaMonitoringDB.COL_FLAG2,AreaMonitoringDB.COL_FLAG3,AreaMonitoringDB.COL_NUMBER };
final String id = "%1%";
cur = cr.query(AreaMonitoringProvider.CONTENT_URI,
projection,
AreaMonitoringDB.ID + " LIKE ?",
new String[] {id},
null);
cur.moveToFirst();
LOGCAT:
03-21 02:11:42.590: E/AndroidRuntime(7665): FATAL EXCEPTION: main
03-21 02:11:42.590: E/AndroidRuntime(7665): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:421)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:117)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:49)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.database.CursorWrapper.getInt(CursorWrapper.java:96)
03-21 02:11:42.590: E/AndroidRuntime(7665): at com.notfall.MainActivity$3.onClick(MainActivity.java:324)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.view.View.performClick(View.java:3112)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.view.View$PerformClick.run(View.java:11956)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.os.Handler.handleCallback(Handler.java:587)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.os.Looper.loop(Looper.java:132)
03-21 02:11:42.590: E/AndroidRuntime(7665): at android.app.ActivityThread.main(ActivityThread.java:4025)
03-21 02:11:42.590: E/AndroidRuntime(7665): at java.lang.reflect.Method.invokeNative(Native Method)
03-21 02:11:42.590: E/AndroidRuntime(7665): at java.lang.reflect.Method.invoke(Method.java:491)
03-21 02:11:42.590: E/AndroidRuntime(7665): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
03-21 02:11:42.590: E/AndroidRuntime(7665): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
03-21 02:11:42.590: E/AndroidRuntime(7665): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
您应该使用ContentProvider
update()
方法:
SQLiteDatabase sqlDB = mDB.getWritableDatabase();
int rowsAffected;
return rowsAffected = sqlDB.update(AreaMonitoringDB.TABLE_NAME, values, selection, selectionArgs);
}
如果您要查询特定的Uri
联系人,请使用UriMatcher
(添加要匹配的特定Uris)并在update()
方法中切换其值。
答案 1 :(得分:1)
增加数据库版本。它应该删除当前表并重新创建它以解决任何缺失的列:
private static final int DATABASE_VERSION = 11; //previously 10
答案 2 :(得分:0)
如果您为现有表引入了新列。 首先从模拟器中卸载旧应用程序,然后重新安装应用程序。我认为这会奏效。
答案 3 :(得分:0)
我认为该错误是由于COL_FLAG2
和COL_FLAG3
字段设置为NOT NULL
而更新表时未指定这些列的值。
尝试从表定义中删除NOT NULL
约束,或在更新表时提供值。