如何在 android 中使用有关 sqlite 数据库的 insert() 方法
How to use insert() method about sqlite Database in android
这是我的代码 (MainActivity.java.OnCreate):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating a database
SQLiteDatabase sqLiteDatabase=this.openOrCreateDatabase("DB_NAME",MODE_PRIVATE,null);
//creating a table with one column named 'column' inside of it.
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS tb_name('column'VARCHAR)");
//creating an OnClickListener for a simple button(I havent included the activity_main.xml but I dont think that it is necessary)
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues contentValues=new ContentValues();
contentValues.put("column","this item is inserted");
sqLiteDatabase.insert("table",null,contentValues);
long valueOfInsertion = sqLiteDatabase.insert("table",null,contentValues);
Log.i("button","value of insertion: "+ valueOfInsertion);
}
});
}```
但我一直从 logcat 获取值 -1(查看我的代码的第 18 行以获取更多信息)和异常:
2021-01-16 22:33:58.162 9051-9051/com.rahgozar.d2 E/SQLiteDatabase: Error inserting column=this item is inserted
android.database.sqlite.SQLiteException: near "table": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO table(column) VALUES (?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1699)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1570)
at com.rahgozar.d2.MainActivity.onClick(MainActivity.java:30)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access00(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
重要说明:仅当我单击按钮(调用 OnClickListener)时才会发生异常;
table 的名字是 tb_name
而不是 table
。
此外,将 column
之类的关键字用于标识符(例如列名)也不是一个好习惯。
最后 SQLite 中没有 VARCHAR
数据类型。您可以使用 TEXT
.
因此,要创建 table 你可以这样做:
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS tb_name(columnName TEXT)");
把columnName
改成有意义的名字(不需要单引号)
并插入新行:
ContentValues contentValues = new ContentValues();
contentValues.put("columnName", "this item is inserted");
sqLiteDatabase.insert("tb_name", null, contentValues);
完成这些更改后,您可能需要从设备上卸载应用程序并重新运行。
这是我的代码 (MainActivity.java.OnCreate):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//creating a database
SQLiteDatabase sqLiteDatabase=this.openOrCreateDatabase("DB_NAME",MODE_PRIVATE,null);
//creating a table with one column named 'column' inside of it.
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS tb_name('column'VARCHAR)");
//creating an OnClickListener for a simple button(I havent included the activity_main.xml but I dont think that it is necessary)
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues contentValues=new ContentValues();
contentValues.put("column","this item is inserted");
sqLiteDatabase.insert("table",null,contentValues);
long valueOfInsertion = sqLiteDatabase.insert("table",null,contentValues);
Log.i("button","value of insertion: "+ valueOfInsertion);
}
});
}```
但我一直从 logcat 获取值 -1(查看我的代码的第 18 行以获取更多信息)和异常:
2021-01-16 22:33:58.162 9051-9051/com.rahgozar.d2 E/SQLiteDatabase: Error inserting column=this item is inserted
android.database.sqlite.SQLiteException: near "table": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO table(column) VALUES (?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1699)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1570)
at com.rahgozar.d2.MainActivity.onClick(MainActivity.java:30)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access00(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
重要说明:仅当我单击按钮(调用 OnClickListener)时才会发生异常;
table 的名字是 tb_name
而不是 table
。
此外,将 column
之类的关键字用于标识符(例如列名)也不是一个好习惯。
最后 SQLite 中没有 VARCHAR
数据类型。您可以使用 TEXT
.
因此,要创建 table 你可以这样做:
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS tb_name(columnName TEXT)");
把columnName
改成有意义的名字(不需要单引号)
并插入新行:
ContentValues contentValues = new ContentValues();
contentValues.put("columnName", "this item is inserted");
sqLiteDatabase.insert("tb_name", null, contentValues);
完成这些更改后,您可能需要从设备上卸载应用程序并重新运行。