android.database.sqlite.SQLiteException:near“)”:语法错误(代码1):

时间:2021-11-11 01:45:30

I am trying to create a table in DB, but I am getting SQLite exception.I have tried almost everything to fix it, but of no use.I dont know why am I getting this error. Removing comma at the end gives another exception : android.database.sqlite.SQLiteException: table products has no column named _cost (code 1):
Any suggestion is welcome.
how do I fix it ?

我试图在数据库中创建一个表,但我得到SQLite异常。我已经尝试了几乎所有东西来修复它,但没有用。我不知道为什么我得到这个错误。删除末尾的逗号给出了另一个例外:android.database.sqlite.SQLiteException:table products没有名为_cost的列(代码1):欢迎提出任何建议。我如何解决它 ?

This is my DB handler class :

这是我的数据库处理程序类:

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) 

Line 38 is db.execSQL(query); and line 57 is SQLiteDatabase db = getWritableDatabase();

第38行是db.execSQL(查询);第57行是SQLiteDatabase db = getWritableDatabase();

4 个解决方案

#1


2  

change your query to this , you're missing some spaces and also you must remove the last comma from the query

将您的查询更改为此,您缺少一些空格,您还必须从查询中删除最后一个逗号

@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);


 }

#2


0  

replace your query with this

用这个替换你的查询

        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 " +
                 ");" ;

actually you are putting one extra , at the end of query.

实际上你在查询结束时放了一个额外的。

#3


0  

Change your query to this

将您的查询更改为此

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 " +
             ");" ;

You have formatting errors in there. An extra comma and two column names concatenated with their column types. See for e.g. how _costINTEGER should be _cost INTEGER

你有格式错误。一个额外的逗号和两个列名称与其列类型连接在一起。参见例如_costINTEGER应该如何_cost INTEGER

#4


0  

Change AUTOINCREMENT to AUTO_INCREMENT and remove extra ',' after column COST_WORKSHOP

将AUTOINCREMENT更改为AUTO_INCREMENT并在列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 " +
             ");" ;

#1


2  

change your query to this , you're missing some spaces and also you must remove the last comma from the query

将您的查询更改为此,您缺少一些空格,您还必须从查询中删除最后一个逗号

@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);


 }

#2


0  

replace your query with this

用这个替换你的查询

        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 " +
                 ");" ;

actually you are putting one extra , at the end of query.

实际上你在查询结束时放了一个额外的。

#3


0  

Change your query to this

将您的查询更改为此

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 " +
             ");" ;

You have formatting errors in there. An extra comma and two column names concatenated with their column types. See for e.g. how _costINTEGER should be _cost INTEGER

你有格式错误。一个额外的逗号和两个列名称与其列类型连接在一起。参见例如_costINTEGER应该如何_cost INTEGER

#4


0  

Change AUTOINCREMENT to AUTO_INCREMENT and remove extra ',' after column COST_WORKSHOP

将AUTOINCREMENT更改为AUTO_INCREMENT并在列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 " +
             ");" ;