DATA NOT INSERT,导致近表语法错误

时间:2016-10-27 11:20:42

标签: android sqlite android-sqlite

点击“AddData”按钮后,应用程序显示“DATA NOT INSERT” 如何解决这个问题?

谁知道在哪里可以获得学习SQLite的好教程?

E/SQLiteLog: (1) near "table": syntax error
     E/SQLiteDatabase: Error inserting NAME=hi MARKS=98 SURNAME=ju
     android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: INSERT INTO student.table(NAME,MARKS,SURNAME) VALUES (?,?,?)
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
    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.insertWithOnConflict(SQLiteDatabase.java:1469)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
    at com.example.user.sqliteapp2.DatabaseHelper.insertData(DatabaseHelper.java:42)
    at com.example.user.sqliteapp2.MainActivity$1.onClick(MainActivity.java:32)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    V/RenderScript: 0x7f27c755c000 Launching thread(s), CPUs 2
    E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f27cf39e610

这是我的代码。

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME ="student.db";
public static final String TABLE_NAME ="student.table";
public static final String COL_1="ID";
public static final String COL_2="NAME";
public static final String COL_3="SURNAME";
public static final String COL_4="MARKS";
public DatabaseHelper(Context context){
    super(context,DATABASE_NAME,null,1);

}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("Create Table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS TEXT);");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXIST"+TABLE_NAME);
    onCreate(db);
}
public boolean insertData(String name,String surname,String marks){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(COL_2,name);
    contentValues.put(COL_3,surname);
    contentValues.put(COL_4,marks);
    long result=db.insert(TABLE_NAME,null,contentValues);
    if(result==-1)
        return false;
    else
        return true;
}
    }

有人能帮助我吗?

1 个答案:

答案 0 :(得分:-1)

使用此行创建表: -

 db.execSQL("CREATE TABLE " + TABLE_NAME + "("
                + COL_1+ " INTEGER," + COL_2 + " TEXT," + COL_3+ " TEXT," + COL_4 + " TEXT" + ")";);

请注意编写代码。因为错误的语法会产生错误。所以请编写与我写的相同的代码。