0

这可能是一个愚蠢的问题,但我在寻找错误时遇到了麻烦。我在“ TEXT”附近有一个语法错误,但我不知道在哪里。我很确定我输入正确。

public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_UNIQUE + " TEXT NOT NULL, " +
                KEY_USERNAME + " TEXT NOT NULL, " +
                KEY_PASSWORD + " TEXT NOT NULL, " +
                KEY_IMAGE  + " TEXT NOT NULL, " +
                KEY_FULLNAME + " TEXT NOT NULL, " +
                KEY_BDAY + " TEXT NOT NULL, " +
                KEY_AGE + " TEXT NOT NULL, " +
                KEY_SEX + " TEXT NOT NULL, " +
                KEY_EMAIL + " TEXT NOT NULL, " +
                KEY_WORK + " TEXT NOT NULL, " +
                KEY_PROF + " TEXT NOT NULL, " +
                KEY_FBUSERNAME + " TEXT NOT NULL, " +
                KEY_FBPASSWORD + " TEXT NOT NULL, " +
                KEY_YMUSERNAME + " TEXT NOT NULL, " +
                KEY_YMPASSWORD + " TEXT NOT NULL, " +
                KEY_GMUSERNAME + " TEXT NOT NULL, " +
                KEY_GMPASSWORD + " TEXT NOT NULL, " +
                KEY_SKUSERNAME + " TEXT NOT NULL, " +
                KEY_SKPASSWORD + " TEXT NOT NULL);"
                );  
    }
4

2 回答 2

2

我试过你的代码,它工作正常,你可以试试下面的代码(更改数据库、表和列的名称,因为我使用了测试名称)

public class dbHandler extends SQLiteOpenHelper{

 // Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "temp_database";



private static final String DATABASE_TABLE ="a";
private static final String KEY_ROWID ="b";
private static final String KEY_UNIQUE = "c";
private static final String KEY_USERNAME = "d";
private static final String KEY_PASSWORD = "e";
private static final String KEY_IMAGE = "f";
private static final String KEY_FULLNAME = "g";
private static final String KEY_BDAY = "h";
private static final String KEY_AGE = "i";
private static final String KEY_SEX = "j";
private static final String KEY_EMAIL = "k";
private static final String KEY_WORK = "l";
private static final String KEY_PROF = "m";
private static final String KEY_FBUSERNAME = "n";
private static final String KEY_FBPASSWORD = "o";
private static final String KEY_YMPASSWORD = "p";
private static final String KEY_YMUSERNAME = "q";
private static final String KEY_GMUSERNAME = "r";
private static final String KEY_GMPASSWORD = "s";
private static final String KEY_SKUSERNAME = "t";
private static final String KEY_SKPASSWORD = "u";

public dbHandler(Context context) {
     super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.w("jndfjfs","this constructor is called");
}

@Override
public void onCreate(SQLiteDatabase db) {


     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_UNIQUE + " TEXT NOT NULL, " +
                KEY_USERNAME + " TEXT NOT NULL, " +
                KEY_PASSWORD + " TEXT NOT NULL, " +
                KEY_IMAGE  + " TEXT NOT NULL, " +
                KEY_FULLNAME + " TEXT NOT NULL, " +
                KEY_BDAY + " TEXT NOT NULL, " +
                KEY_AGE + " TEXT NOT NULL, " +
                KEY_SEX + " TEXT NOT NULL, " +
                KEY_EMAIL + " TEXT NOT NULL, " +
                KEY_WORK + " TEXT NOT NULL, " +
                KEY_PROF + " TEXT NOT NULL, " +
                KEY_FBUSERNAME + " TEXT NOT NULL, " +
                KEY_FBPASSWORD + " TEXT NOT NULL, " +
                KEY_YMUSERNAME + " TEXT NOT NULL, " +
                KEY_YMPASSWORD + " TEXT NOT NULL, " +
                KEY_GMUSERNAME + " TEXT NOT NULL, " +
                KEY_GMPASSWORD + " TEXT NOT NULL, " +
                KEY_SKUSERNAME + " TEXT NOT NULL, " +
                KEY_SKPASSWORD + " TEXT NOT NULL);"
                );  
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

}

您可以从其他活动中访问您的数据库,例如,

dbHandler mydb=new dbHandler(this);
    mydb.getWritableDatabase();
于 2013-01-01T11:23:00.450 回答
0

由于您尚未发布 Logcat,因此我们无法发现问题。

但是,一个方便的建议:如果您是数据库 CRUD 操作的专家,那么编写查询并使用rawQuery()方法执行它。

db.rawQuery("CREATE TABLE IF NOT EXISTS myTable (username TEXT, password TEXT)", null);
于 2013-01-01T10:48:05.187 回答