[英]error occured when I try to insert data into sqlte
So I recently learn how to insert data in android using sqlite , and this error occurred, I don't know why this error occurred, I have tried to reread my book and I don't have any solution from it.所以我最近学习了如何使用 sqlite 在 android 中插入数据,并且发生了这个错误,我不知道为什么会发生这个错误,我试图重读我的书,但我没有任何解决方案。
I've reread my book and find the problem on the internet.我重读了我的书,并在互联网上找到了问题。
SQLiteDatabase db;
public DBAdapter(Context context,String name, SQLiteDatabase.CursorFactory
factory, int version){
super(context,DATABASE_NAME,factory,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_TABLE = "CREATE TABLE" + TABLE_CONTACTS + "(" + nomber +
"INTEGER PRIMARYKEY," + namapen + "TEXT," + idPen + "INTEGER,"
+ namaGej + "TEXT," + idGej + "TEXT )";
db.execSQL(CREATE_TABLE);
}
The error in logcat : logcat 中的错误:
Process: com.example.lordbramasta.pakar, PID: 20404
android.database.sqlite.SQLiteException: near "TABLEtb_penyakit": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLEtb_penyakit(nomberINTEGER PRIMARYKEY,namapenTEXT,idPenINTEGER,namaGejTEXT,idGejTEXT )
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:901)
I expected to insert data into sqlite database.我希望将数据插入到 sqlite 数据库中。
Change :-改变 :-
public void onCreate(SQLiteDatabase db){
String CREATE_TABLE = "CREATE TABLE" + TABLE_CONTACTS + "(" + nomber +
"INTEGER PRIMARYKEY," + namapen + "TEXT," + idPen + "INTEGER,"
+ namaGej + "TEXT," + idGej + "TEXT )";
db.execSQL(CREATE_TABLE);
}
to到
public void onCreate(SQLiteDatabase db){
String CREATE_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + nomber +
" INTEGER PRIMARY KEY," + namapen + " TEXT," + idPen + " INTEGER,"
+ namaGej + " TEXT," + idGej + " TEXT )";
db.execSQL(CREATE_TABLE);
}
Then either :-那么要么:-
and then rerun the App.然后重新运行应用程序。
The reason why the issue didn't arise until you attempted to insert is because that would have been the first attempt that the database would have been opened.直到您尝试插入才出现问题的原因是因为这将是数据库打开的第一次尝试。 Instantiating the Database Helper doesn't result in the database being created (ie the onCreate method being executed, onCreate is only automatically executed when an attempt is made to open the database (implicit with an insert) and the database doesn't actually exist).
实例化 Database Helper 不会导致创建数据库(即执行onCreate方法,只有在尝试打开数据库(隐含插入)并且数据库实际上不存在时才会自动执行onCreate ) .
The reason why the App's data should be deleted (or the App is uninstalled, which will delete the App's data) is that this deletes the database and thus the onCreate method will run (it doesn't run when the database exists).之所以应该删除App的数据(或者卸载App,会删除App的数据)是因为这样会删除数据库,因此onCreate方法会运行(数据库存在时不会运行)。
The actual error is because CREATE expects the type of what is to be created in your case CREATE TABLE table_name
whilst the SQL you have resolved to CREATE TABLEtb_penyakit
so you are trying to tell it to create a CREATE TABLEtb_penyakit and to SQlite there is no such thing as a CREATE TABLEtb_penyakit .实际的错误是因为 CREATE 期望在您的情况下
CREATE TABLE table_name
的类型CREATE TABLE table_name
而您已解析为CREATE TABLEtb_penyakit
的 SQL 所以您试图告诉它创建一个CREATE TABLEtb_penyakit和 SQlite 没有这样的事情作为CREATE TABLEtb_penyakit 。
Without the other spaces that have been added in the above, the table would have been created BUT the columns would have been :-如果没有在上面添加的其他空格,表将被创建,但列将是:-
The insert would then have failed, as the columns that you expected to exist would not have existed.插入将失败,因为您期望存在的列将不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.