添加一个表到现有的SQLite数据库

问题描述:

所以我很忙的投票应用程序,我试图添加一个表到现有的数据库只是为了存储注册人的地址信息,但当我运行应用程序时出现错误在我的手机上,当我点击登录或注册按钮的应用程序只是崩溃,我用日志猫,但因为我是一个新的时间android程序员,我不知道我的错误。我得到的错误是这样的:添加一个表到现有的SQLite数据库

10-19 14:58:52.170 29644-29644/com.example.loginregisterwithsqlite E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.loginregisterwithsqlite, PID: 29644 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginregisterwithsqlite/com.example.loginregisterwithsqlite.LoginAndRegistrion}: android.database.sqlite.SQLiteException: near "tableADDRESS": syntax error (code 1): , while compiling: create tableADDRESS(ID integer primary key autoincrement,PROVINCE text,MUNICIPALITY text,STREET text,CITY text,FLAT text,TRIBE text,SUBURB text,TOWN text,POST text) 
################################################################# 
Error Code : 1 (SQLITE_ERROR) 
Caused By : SQL(query) error or missing database. 
    (near "tableADDRESS": syntax error (code 1): , while compiling: create tableADDRESS(ID integer primary key autoincrement,PROVINCE text,MUNICIPALITY text,STREET text,CITY text,FLAT text,TRIBE text,SUBURB text,TOWN text,POST text)) 
################################################################# 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008) 
    at android.app.ActivityThread.-wrap14(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6688) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) 
Caused by: android.database.sqlite.SQLiteException: near "tableADDRESS": syntax error (code 1): , while compiling: create tableADDRESS(ID integer primary key autoincrement,PROVINCE text,MUNICIPALITY text,STREET text,CITY text,FLAT text,TRIBE text,SUBURB text,TOWN text,POST text) 
################################################################# 
Error Code : 1 (SQLITE_ERROR) 
Caused By : SQL(query) error or missing database. 
    (near "tableADDRESS": syntax error (code 1): , while compiling: create tableADDRESS(ID integer primary key autoincrement,PROVINCE text,MUNICIPALITY text,STREET text,CITY text,FLAT text,TRIBE text,SUBURB text,TOWN text,POST text)) 
################################################################# 
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1005) 
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:570) 
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59) 
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1976) 
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1907) 
    at com.example.loginregisterwithsqlite.DataBaseHelper.onCreate(DataBaseHelper.java:16) 
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251) 
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) 
    at com.example.loginregisterwithsqlite.LoginDataBaseAdapter.open(LoginDataBaseAdapter.java:44) 
    at com.example.loginregisterwithsqlite.LoginAndRegistrion.onCreate(LoginAndRegistrion.java:38) 
    at android.app.Activity.performCreate(Activity.java:6912) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2900) 
    ... 9 more 

和我插入到表到数据库代码如下:

public class LoginDataBaseAdapter { 

static final String DATABASE_NAME = "votersystem.db"; 
static final int DATABASE_VERSION = 1; 
public static final int NAME_COLUMN = 4; 

static final String CREATE_ADDRESS_TABLE = "create table"+"ADDRESS"+ 
     "("+"ID integer primary key autoincrement,"+"PROVINCE text," +"MUNICIPALITY text,"+"STREET text,"+"CITY text,"+"FLAT text,"+"TRIBE text,"+"SUBURB text,"+"TOWN text,"+"POST text) "; 

static final String CREATE_LOGIN_TABLE = "create table "+"LOGIN"+ 
     "(" +"ID integer primary key autoincrement,"+"UNAME text," +"EMAIL text,"+"IDNUMBER text,"+"UPHONE text,"+"DIS text," +"PASSWORD text,"+"REPASSWORD text,"+ "SECURITYHINT text) "; 

static final String CREATE_ADMIN_TABLE = "create table "+"ADMIN"+ 
     "(" +"ID integer primary key autoincrement,"+ "FIRSTNAME text,"+"LASTNAME text,"+"PARTY text,"+ "SYMBOL text) "; 

static final String CREATE_REPORT_TABLE = "create table "+"REPORT"+"(" +"ID integer primary key autoincrement,"+ "FULLNAME text,"+"PARTY text"+")"; 
public SQLiteDatabase db; 
private final Context context; 
private DataBaseHelper dbHelper; 
private Cursor mCursor; 

public LoginDataBaseAdapter(Context _context) 
{ 
    context = _context; 
    dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION); 

} 
public LoginDataBaseAdapter open() throws SQLException 
{ 
    db = dbHelper.getWritableDatabase(); 
    return this; 
} 
public void close() 
{ 
    db.close(); 
} 

public SQLiteDatabase getDatabaseInstance() 
{ 
    return db; 
} 

public void insertEntry(String Prov, String Mun, String str, String cty, String flt, String trb, String sburb, String twn, String pst) 
{ 
    ContentValues newValues = new ContentValues(); 
    newValues.put("PROVINCE",Prov); 
    newValues.put("MUNICIPALITY",Mun); 
    newValues.put("STREET",str); 
    newValues.put("CITY",cty); 
    newValues.put("FLAT",flt); 
    newValues.put("TRIBE",trb); 
    newValues.put("SUBURB",sburb); 
    newValues.put("TOWN",twn); 
    newValues.put("POST",pst); 

    db.insert("ADDRESS", null, newValues); 
} 

我的错误是在CREATE_ADDRESS_TABLE请可能有人帮助我吗?

+0

您应该先将'tableADDRESS'更改为:'table ADDRESS'。 - >'“创建表”+“ADDRESS”' – FlanschiFox

您的查询是不正确的,因为你缺少一个空格:

“创建表” + “地址” +

将其更改为:

"create table "+"ADDRESS" 

你是在你的其他查询中纠正它,所以这可能是你没有注意到的一个小错误。

请参阅您的堆栈跟踪下面一行:

产生的原因:SQL(查询)错误或丢失的数据库。 (附近“tableADDRESS”:语法错误(代码1):,编译时:创建 tableADDRESS(ID整数主键自动增量,PROVINCE 文本,市文本,街道文本,CITY文本,FLAT文本,TRIBE 文本,SUBURB文本,TOWN文本,POST文本))