语法错误(代码1):,编译时:INSERT INTO rest_tables(0,TABLENUMBER)VALUES(?,?)

时间:2018-07-22 08:28:40

标签: java android sqlite

我试图在Android Studio中使用sqlite创建表,但是当我单击将数据添加到表的按钮时,出现此错误。

07-22 14:19:01.836 23818-23818/com.example.user.restapp E/SQLiteLog: (1) no such table: rest_tables
07-22 14:19:01.851 23818-23818/com.example.user.restapp E/SQLiteDatabase: Error inserting Tablenumber=2 Taken=0
07-22 11:12:20.576 21924-21924/com.example.user.restapp E/SQLiteLog: (1) near "0": syntax error
07-22 11:12:20.596 21924-21924/com.example.user.restapp E/SQLiteDatabase: Error inserting 0=0 TABLENUMBER=1
    android.database.sqlite.SQLiteException: near "0": syntax error (code 1): , while compiling: INSERT INTO rest_tables(0,TABLENUMBER) VALUES (?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1590)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1462)
        at com.example.user.restapp.DatabaseHelper.InsertData(DatabaseHelper.java:39)

这是 DatabaseHelper 类:

public static final String TABLE_NAME = "rest_tables";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "TABLENUMBER";
    public static final String TAKEN = "0";

public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,TABLENUMBER TEXT,TAKEN TEXT)");
           // db.execSQL("CREATE TABLE " + TABLE_NAME + "()");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public boolean InsertData(String tablenumber,String taken)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,tablenumber);
        contentValues.put(TAKEN,taken);
        long result = db.insert(TABLE_NAME,null,contentValues);
        if(result == -1)
            return false;
        else
            return true;
    }

在MainActivity类中按钮的OnClickListener中,我写了:

boolean isInserted = myDB.InsertData("2","0");
                if(isInserted == true)
                    Toast.makeText(getApplicationContext(), "Table Added successfully", Toast.LENGTH_LONG).show();
                else
                    Toast.makeText(getApplicationContext(), "Table Not Added", Toast.LENGTH_LONG).show();

有人可以告诉我究竟是什么导致了此错误吗?

2 个答案:

答案 0 :(得分:1)

public static final String TAKEN = "0";

...

contentValues.put(TAKEN,taken);

0不是有效的列名,这是语法错误的直接原因。

与您的架构相对应的列名将是"TAKEN"而不是"0"

答案 1 :(得分:0)

尝试一下:

public  SQLiteDatabase db;
private final Context context;
private DataBaseHelper dbHelper;

public  DataBaseAdapter(Context _context) 
{
    context = _context;
    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);

}
public  DataBaseAdapter open() throws SQLException 
{
    db = dbHelper.getWritableDatabase();
    return this;
}
public void close() 
{
    db.close();
}

public  SQLiteDatabase getDatabaseInstance()
{
    return db;
}

public boolean InsertData(String tablenumber,String taken)
    {

        ContentValues contentValues = new ContentValues();
        contentValues.put("TABLENUMBER",tablenumber);
        contentValues.put("TAKEN",taken);
         db.insert(TABLE_NAME,null,contentValues);

以及您的主要活动中:

//if statement
    DataBaseAdapter.InsertData(f2","0");
                        Toast.makeText(activity.this, "Data save successfully !", Toast.LENGTH_LONG).show();

                }


            }else {
                Toast.makeText(activity.this, "Never insert!", Toast.LENGTH_LONG).show();
            }