Android 创建 table 时出现 SQLite 错误(VARCHAR 导致错误)
Android SQLite error when creating a table (VARCHAR is causing an error)
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = this.openOrCreateDatabase("Practice", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR");
}
错误日志:
E/SQLiteLog: (1) near "VARCHAR": syntax error 06-30 23:46:21.834
11420-11420/com.asd.sqltesting D/AndroidRuntime: Shutting down VM
06-30 23:46:21.835 11420-11420/com.asd.sqltesting E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.asd.sqltesting, PID: 11420
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.asd.sqltesting/com.asd.sqltesting.MainActivity}:
android.database.sqlite.SQLiteException: near "VARCHAR": syntax error
(code 1): , while compiling: CREATE TABLE IF NOT EXISTS blah (subjects
VARCHAR, notes VARCHAR, amount VARCHAR
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: near "VARCHAR":
syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS
blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR
at
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method)
at
android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at
android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
at
android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
at
android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
at
android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.asd.sqltesting.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Activity.java:5933)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(ActivityThread.java:144)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我试图创建一个包含多个列的 table,但这种情况一直在发生。这行代码用于另一个项目,但我第二次在同一个 activity 中使用它时出现错误。
你错过了结尾')':
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR);");
VARCHAR 的大小在 sqlite 中是可选的
参见 https://www.sqlite.org/syntax/type-name.html。
要验证您的陈述,您可以使用 sqlite3
$ sqlite3
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
Connected to a transient in-memory database.
sqlite> CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR);
您缺少圆括号。
这样写您的查询:
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR)");
Insert a )
after amount VARCHAR
编辑:1
android.database.sqlite.SQLiteException : (1) near "VARCHAR": syntax error
Your logcat is displaying this Exception. So generally this exception
arises whenever you are missing some character like ,
or "
or in
your case it is )
. Observe your query you will find two opening
round brackets (
, One before Create table & 2nd after blah.
But there is only one closing bracket )
at the end. This is the
reason i have suggested you to add one )
after amount VARCHAR
.
您的查询有误,您漏掉了结束符 ')':
不要直接在 MainActivity 中创建数据库,遵循以下方法并始终通过异步任务调用数据库,
使用 SQLiteOpenHelper 的最佳实践,参考下面的 bolg
https://github.com/codepath/android_guides/wiki/Local-Databases-with-SQLiteOpenHelper
示例项目
- http://www.ssaurel.com/blog/learn-to-persist-data-on-android-with-sqlite/
- http://www.androidwarriors.com/2016/02/android-sqlite-database-tutorial-sqlite.html
创建 table 如下所示,
样本:
String CREATE_POSTS_TABLE = "CREATE TABLE " + TABLE_POSTS +
"(" +
KEY_POST_ID + " INTEGER PRIMARY KEY," + // Define a primary key
KEY_POST_USER_ID_FK + " INTEGER REFERENCES " + TABLE_USERS + "," + // Define a foreign key
KEY_POST_TEXT + " TEXT" +
")";
String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS +
"(" +
KEY_USER_ID + " INTEGER PRIMARY KEY," +
KEY_USER_NAME + " TEXT," +
KEY_USER_PROFILE_PICTURE_URL + " TEXT" +
")";
db.execSQL(CREATE_POSTS_TABLE);
db.execSQL(CREATE_USERS_TABLE);
sql
查询错误。
先更正一下
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR(size), notes VARCHAR(size), amount VARCHAR(size))");
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = this.openOrCreateDatabase("Practice", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR");
}
错误日志:
E/SQLiteLog: (1) near "VARCHAR": syntax error 06-30 23:46:21.834 11420-11420/com.asd.sqltesting D/AndroidRuntime: Shutting down VM 06-30 23:46:21.835 11420-11420/com.asd.sqltesting E/AndroidRuntime: FATAL EXCEPTION: main Process: com.asd.sqltesting, PID: 11420 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asd.sqltesting/com.asd.sqltesting.MainActivity}: android.database.sqlite.SQLiteException: near "VARCHAR": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access0(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: android.database.sqlite.SQLiteException: near "VARCHAR": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605) at com.asd.sqltesting.MainActivity.onCreate(MainActivity.java:18) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access0(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我试图创建一个包含多个列的 table,但这种情况一直在发生。这行代码用于另一个项目,但我第二次在同一个 activity 中使用它时出现错误。
你错过了结尾')':
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR);");
VARCHAR 的大小在 sqlite 中是可选的
参见 https://www.sqlite.org/syntax/type-name.html。
要验证您的陈述,您可以使用 sqlite3
$ sqlite3
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
Connected to a transient in-memory database.
sqlite> CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR);
您缺少圆括号。
这样写您的查询:
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR, notes VARCHAR, amount VARCHAR)");
Insert a
)
afteramount VARCHAR
编辑:1
android.database.sqlite.SQLiteException : (1) near "VARCHAR": syntax error
Your logcat is displaying this Exception. So generally this exception arises whenever you are missing some character like
,
or"
or in your case it is)
. Observe your query you will find two opening round brackets(
, One before Create table & 2nd after blah. But there is only one closing bracket)
at the end. This is the reason i have suggested you to add one)
afteramount VARCHAR
.
您的查询有误,您漏掉了结束符 ')': 不要直接在 MainActivity 中创建数据库,遵循以下方法并始终通过异步任务调用数据库,
使用 SQLiteOpenHelper 的最佳实践,参考下面的 bolg
https://github.com/codepath/android_guides/wiki/Local-Databases-with-SQLiteOpenHelper
示例项目
- http://www.ssaurel.com/blog/learn-to-persist-data-on-android-with-sqlite/
- http://www.androidwarriors.com/2016/02/android-sqlite-database-tutorial-sqlite.html
创建 table 如下所示,
样本:
String CREATE_POSTS_TABLE = "CREATE TABLE " + TABLE_POSTS +
"(" +
KEY_POST_ID + " INTEGER PRIMARY KEY," + // Define a primary key
KEY_POST_USER_ID_FK + " INTEGER REFERENCES " + TABLE_USERS + "," + // Define a foreign key
KEY_POST_TEXT + " TEXT" +
")";
String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS +
"(" +
KEY_USER_ID + " INTEGER PRIMARY KEY," +
KEY_USER_NAME + " TEXT," +
KEY_USER_PROFILE_PICTURE_URL + " TEXT" +
")";
db.execSQL(CREATE_POSTS_TABLE);
db.execSQL(CREATE_USERS_TABLE);
sql
查询错误。
先更正一下
db.execSQL("CREATE TABLE IF NOT EXISTS blah (subjects VARCHAR(size), notes VARCHAR(size), amount VARCHAR(size))");