创建sqlite表

时间:2013-01-01 10:44:25

标签: android

这可能是一个愚蠢的问题,但我在查找错误时遇到了麻烦。我在“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);"
                );  
    }

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

由于你还没有发布Logcat,我们无法解决这个问题。

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

db.rawQuery("CREATE TABLE IF NOT EXISTS myTable (username TEXT, password TEXT)", null);