我遇到插入查询问题。当我尝试插入数据库时,它在日志cat中说active:
列不存在。你能指出我弄错了吗?
我实际上得到了这个例外:
SQLiteException: table video has no column named active: , while compiling: insert into video (fav, path, subcat_id , video_name, active) VALUES ('0','/mnt/sdcard/Samsung','videocat','Samsung','1')
这是我的SQLITEHELPER类的代码
public class DatabaseHelper extends SQLiteOpenHelper {
//changing master
// Logcat tag
//add comment
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 9;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
// Table Names
private static final String TABLE_VIDEO = "video";
private static final String TABLE_PICTURE = "picture";
private static final String TABLE_MUSIC = "music";
// Common column names
private static final String KEY_ID = "id";
private static final String KEY_FAV = "fav";
private static final String KEY_ACTIVE = "active";
// MUSIC Table - column names
private static final String KEY_MUSIC_PATH = "path";
private static final String KEY_MUSIC_ALBUM = "is_album";
private static final String KEY_MUSIC_NAME = "music_name";
// PICTURE Table - column names
private static final String KEY_PICTURE_PATH = "path";
private static final String KEY_PICTURE_ALBUM = "is_album";
private static final String KEY_PICTURE_NAME = "picture_name";
// VIDEO Table - column names
private static final String KEY_VIDEO_PATH = "path";
private static final String KEY_VIDEO_SUBCAT_ID = "subcat_id";
private static final String KEY_VIDEO_NAME = "video_name";
// Table Create Statements
// Video table create statement
private static final String CREATE_TABLE_VIDEO = "CREATE TABLE "
+ TABLE_VIDEO + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_FAV + " INTEGER,"
+ KEY_VIDEO_PATH + " TEXT,"
+ KEY_VIDEO_SUBCAT_ID + " TEXT,"
+ KEY_VIDEO_NAME + " TEXT"
+ KEY_ACTIVE + "INTEGER)";
// PICTURE table create statement
private static final String CREATE_TABLE_PICTURE = "CREATE TABLE "
+ TABLE_PICTURE + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_FAV + " INTEGER,"
+ KEY_PICTURE_PATH + " TEXT,"
+ KEY_PICTURE_ALBUM+ " INTEGER,"
+ KEY_PICTURE_NAME + " TEXT"
+ KEY_ACTIVE + "INTEGER)";
// MUSIC table create statement
private static final String CREATE_TABLE_MUSIC = "CREATE TABLE "
+ TABLE_MUSIC + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_FAV + " INTEGER,"
+ KEY_MUSIC_PATH + " TEXT,"
+ KEY_MUSIC_ALBUM+ " INTEGER,"
+ KEY_MUSIC_NAME + " TEXT"
+ KEY_ACTIVE + "INTEGER)";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
// creating required tables
db.execSQL(CREATE_TABLE_PICTURE);
db.execSQL(CREATE_TABLE_MUSIC);
db.execSQL(CREATE_TABLE_VIDEO);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_VIDEO);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PICTURE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_MUSIC);
// create new tables
onCreate(db);
}
// add videos
public void addvideos(Video item, int active, String subcat) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("insert into video (fav, path, subcat_id , video_name, active) VALUES (" + "'" + item.getFavourite() + "',"+ "'" + item.getPath() + "'," + "'" + subcat + "'," + "'" + item.getName() +"'," + "'" + active + "'" + ")");
}
// add pictures
public void addpictures(Picture item, int is_album, int active) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("insert into picture (fav, path, is_album , picture_name, active) VALUES (" + "'" + item.getFavourite() + "',"+ "'" + item.getPath() + "'," + "'" + is_album + "'," + "'" + item.getName() +"'," + "'" + active + "'" + ")");
}
// add music
public void addmusic(Music item, int catid, int active) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("insert into music (fav, path, cat_id , video_name, active) VALUES (" + "'" + item.getFavourite() + "',"+ "'" + item.getPath() + "'," + "'" + catid + "'," + "'" + item.getName() +"'," + "'" + active + "'" + ")");
}
,这是我的Log Cat
02-19 17:21:46.842: E/AndroidRuntime(15469): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tvdashboard.database/com.tvdashboard.main.VideoSection}: android.database.sqlite.SQLiteException: table video has no column named active: , while compiling: insert into video (fav, path, subcat_id , video_name, active) VALUES ('0','/mnt/sdcard/Samsung','videocat','Samsung','1')
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.app.ActivityThread.access$600(ActivityThread.java:128)
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.os.Looper.loop(Looper.java:137)
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.app.ActivityThread.main(ActivityThread.java:4514)
02-19 17:21:46.842: E/AndroidRuntime(15469): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 17:21:46.842: E/AndroidRuntime(15469): at java.lang.reflect.Method.invoke(Method.java:511)
02-19 17:21:46.842: E/AndroidRuntime(15469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
02-19 17:21:46.842: E/AndroidRuntime(15469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-19 17:21:46.842: E/AndroidRuntime(15469): at dalvik.system.NativeStart.main(Native Method)
02-19 17:21:46.842: E/AndroidRuntime(15469): Caused by: android.database.sqlite.SQLiteException: table video has no column named active: , while compiling: insert into video (fav, path, subcat_id , video_name, active) VALUES ('0','/mnt/sdcard/Samsung','videocat','Samsung','1')
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-19 17:21:46.842: E/AndroidRuntime(15469): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
答案 0 :(得分:2)
下面:
+ KEY_ACTIVE + "INTEGER)";
您必须在“INTEGER)”之前插入一个空格;
所以:
+ KEY_ACTIVE + " INTEGER)";
您的代码中有3次此问题。
答案 1 :(得分:0)
您在列名称和数据类型之间缺少空格。实际上,在所有CREATE_TABLE语句中,您都有相同的内容。您需要将“INTEGER”替换为“INTEGER”“
private static final String CREATE_TABLE_VIDEO = "CREATE TABLE "
+ TABLE_VIDEO + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_FAV + " INTEGER,"
+ KEY_VIDEO_PATH + " TEXT,"
+ KEY_VIDEO_SUBCAT_ID + " TEXT,"
+ KEY_VIDEO_NAME + " TEXT"
+ KEY_ACTIVE + " INTEGER)"; // SPACE INSERTED BEFORE INTEGER
希望这有帮助!