我正在尝试在DB中创建一个表,但我得到了SQLite异常。我已经尝试了几乎所有东西来修复它,但没有用。我不知道为什么我得到这个错误。最后删除逗号会另外例外:android.database.sqlite.SQLiteException: table products has no column named _cost (code 1):
欢迎提出任何建议
我如何解决它 ?
这是我的数据库处理程序类:
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME= "products.db";
private static final String TABLE_PRODUCTS = "products";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_PRODUCTNAME = "productname";
private static final String COLUMN_VENUE = "venue";
private static final String COLUMN_DATEOFSTART = "_dateofstart";
private static final String COLUMN_DATEOFEND = "_dateofend";
private static final String DURATION_WORKSHOP= "_duration";
private static final String COST_WORKSHOP = "_cost";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context,DATABASE_NAME, factory,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + "INTEGER, " +
COST_WORKSHOP + "INTEGER, " +
");" ;
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
public void addProduct (Products products){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME,products.get_productname());
values.put(COLUMN_VENUE,products.get_venue());
values.put(COLUMN_DATEOFSTART,products.get_dateofstart());
values.put(COLUMN_DATEOFEND,products.get_dateofend());
values.put(DURATION_WORKSHOP,products.get_duration());
values.put(COST_WORKSHOP,products.get_cost());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_PRODUCTS,null,values);
db.close();
}
LOGCAT:
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT, productname TEXT, venue TEXT, _dateofstart TEXT, _dateofend TEXT, _durationINTEGER, _costINTEGER, );
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1675)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
at com.example.dell_1.sqlite.MyDBHandler.onCreate(MyDBHandler.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.dell_1.sqlite.MyDBHandler.addProduct(MyDBHandler.java:57)
at com.example.dell_1.sqlite.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:6705)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
第38行是db.execSQL(query);
,第57行是SQLiteDatabase db = getWritableDatabase();
答案 0 :(得分:2)
将您的查询更改为此,您缺少一些空格,并且您必须从查询中删除最后一个逗号
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + " INTEGER, " +
COST_WORKSHOP + " INTEGER " +
");" ;
db.execSQL(query);
}
答案 1 :(得分:0)
用此
替换您的查询 String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + "INTEGER, " +
COST_WORKSHOP + "INTEGER " +
");" ;
实际上你在查询结束时增加了一个。
答案 2 :(得分:0)
将AUTOINCREMENT更改为 AUTO_INCREMENT 并删除额外的&#39;,&#39;在列COST_WORKSHOP之后
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTO_INCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + " INTEGER, " +
COST_WORKSHOP + " INTEGER " +
");" ;
答案 3 :(得分:0)
将您的查询更改为此
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT, " +
COLUMN_VENUE + " TEXT, " +
COLUMN_DATEOFSTART + " TEXT, " +
COLUMN_DATEOFEND + " TEXT, " +
DURATION_WORKSHOP + " INTEGER, " +
COST_WORKSHOP + " INTEGER " +
");" ;
那里有格式错误。一个额外的逗号和两个列名称与其列类型连接在一起。参见例如_costINTEGER
应该是_cost INTEGER