我創建了一個SQLite Helper類來處理我的數據庫。android.database.sqlite.SQLiteException:在「table」附近:語法錯誤(代碼1):編譯時:
我在運行我的代碼時經常出現這個錯誤,但是我在創建表語法中找不到任何錯誤。
ERROR: android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling:CREATE TABLE table (ID INTEGER PRIMARY KEY AUTOINCREMENT, ACCOUNT_HOLDER TEXT, DEBIT INTEGER, CREDIT INTEGER, BALANCE INTEGER)
package com.example.user.balancesheet;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper extends SQLiteOpenHelper{
private static final int DB_Version=1;
private static final String Database="BalanceSheet.db";
public static String Table="table";
public DatabaseHelper(Context context) {
super(context, Database, null, DB_Version);
SQLiteDatabase db=this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String query="CREATE TABLE " + Table +" (ID INTEGER PRIMARY KEY
AUTOINCREMENT, ACCOUNT_HOLDER TEXT, DEBIT INTEGER, CREDIT INTEGER,
BALANCE INTEGER)";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP IF EXIST " + Table);
onCreate(db);
}
public Boolean insertdata(String name, int deb, int cred, int bal){
ContentValues val=new ContentValues();
val.put("ACCOUNT_HOLDER", name);
val.put("DEBIT", deb);
val.put("CREDIT_HOLDER", cred);
val.put("BALANCE", bal);
SQLiteDatabase db=this.getWritableDatabase();
long result=db.insert(Table, null, val);
Log.d("METHOD","ADDLEDGER");
if(result==-1)
return false;
else
return true;
}
}
請參閱https://sqlite.org/lang_keywords.html –