我是新来的(以及一般的编程)并且希望你可以帮助我。当我尝试启动应用程序时,它只是“停止工作”。 我在这里查看了所有类似的问题,但无法解决。 这是我的代码:
package com.example.me.mac_xyz;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "mac.db";
public static final String TABLE_NAME = "1";
public static final String COL_1 = "NUMBER";
public static final String COL_2 = "MAC";
public static final String COL_3 = "X";
public static final String COL_4 = "Y";
public static final String COL_5 = "Z";
/* Constructor*/
public DatabaseHelper(Context context) { /*, String name, SQLiteDatabase.CursorFactory factory, int version*/
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + TABLE_NAME +"(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" +TABLE_NAME);
onCreate(db);
}
这是在日志中:
11-12 22:34:47.706 31044-31044/com.example.me.mac_xyz E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.me.mac_xyz, PID: 31044
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.me.mac_xyz/com.example.me.mac_xyz.MainActivity}: android.database.sqlite.SQLiteException: near "table1": syntax error (code 1): , while compiling: create table1(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "table1": syntax error (code 1): , while compiling: create table1(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)
* * *
我做错了什么?
答案 0 :(得分:4)
问题在于您的SQL语句。这就是你的SQL查询对android系统的看法:
create table1(NUMBER INTEGER PRIMARY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER).
Change query to :
create table table_name (NUMBER INTEGER PRIMARY KEY,MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER)
将onCreate更改为:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +
" (NUMBER INTEGER PRIMARY KEY,
MAC TEXT,X INTEGER,Y INTEGER,Z INTEGER);");
}
注意:表名不能是数字,因此将其更改为有效的表名。请参阅:What are valid table names in SQLite?