简体   繁体   English

当我尝试将数据插入 sqlte 时发生错误

[英]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 :-那么要么:-

  • delete the App's data, or删除应用程序的数据,或
  • uninstall the App卸载应用程序

and then rerun the App.然后重新运行应用程序。

Explanations说明

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.

相关问题 try-with-resource is not supported in -source 1.5 当我在源中使用 try catch 时发生错误 - try-with-resource is not supported in -source 1.5 error occured when I used try catch in the source 当我尝试在子表中插入多个数据时发生错误无法执行JDBC批更新 - When I try to insert multiple data into child table then error occurs Could not execute JDBC batch update 当我尝试用数据填充表时出现错误 - error when I try to fill table with data 当未打开应用程序并且尝试在Realm中插入数据时,出现错误“无法在线程上自动更新Realm>没有Looper”? - When app is not opened and when i try to insert Data in Realm I get Error “Realm cannot be automatically updated on a thread > without a looper”? 当我尝试在 SQLite Android Studio 上进行插入时出错 - error when i try to do an insert on SQLite Android Studio 当我尝试将带有 hibernate 的字段插入表中时出错 - Error when I try to insert fields with hibernate into a table 当我尝试o插入数据时dataexecuteQuery()无法正常工作吗? - When I try o insert data dataexecuteQuery() isn't working? 尝试插入空值时,spring出现mybatis错误 - Error in mybatis with spring when try to insert nulls 我尝试在mysql表中插入数据 - I am try to insert data in mysql table 当我尝试从 firebase 更新数据时出错 - Error when i try to update data from firebase
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM