在Android中使用SQLite创建表的问题

时间:2010-11-26 16:35:12

标签: java android sqlite

我的create table语句如下所示:

private static final String USER_CREATE =
 "create table user (uid text unique, pw text, admin integer);";

当我尝试插入数据库时​​,它允许我为uid插入一个值,但是如果我尝试为pw或admin插入一个值,它会抛出一个SQLiteException'表用户没有名为pw'的列,并且管理员也一样我的语法不对吗?这是我的DBAdapter代码:

public class DBAdapter extends Activity {

// * ** * *** DATABASE CONSTANTS * < / EM> ** * ** * ***

public static final String WORDS_TABLE =“words”;  public static final String ATT_WORD =“word”;     public static final String ATT_T1 =“T1”;     public static final String ATT_T2 =“T2”;     public static final String ATT_T3 =“T3”;     public static final String ATT_T4 =“T4”;     public static final String ATT_T5 =“T5”;     public static final String ATT_RECENT =“recent”;     public static final String ATT_RATING =“rating”;

public static final String STATISTICS_TABLE = "statistics";
public static final String ATT_GAMESPLAYED = "gamesPlayed";
public static final String ATT_wordsCORRECT = "wordsCorrect";
public static final String ATT_wordsWRONG = "wordsWrong";
public static final String ATT_POINTTOTAL = "pointTotal";
public static final String ATT_WINS = "wins";
public static final String ATT_LOSSES = "losses";

public static final String SETTING_TABLE = "settings";
public static final String ATT_SID = "sid";
public static final String ATT_TYPE = "type";
public static final String ATT_DURATION = "duration";
public static final String ATT_PTSTOWIN = "ptsToWin";

public static final String CATEGORIES_TABLE = "categories";
public static final String ATT_CATEGORY = "category";

public static final String BELONGS_TABLE = "belongs";

public static final String USER_TABLE = "user";
public static final String ATT_UID = "uid";
public static final String ATT_PW = "pw";
public static final String ATT_ADMIN = "admin";

public static final String DATABASE_NAME = "taboo";
private static final int DATABASE_VERSION = 1;
private static final String TAG = "DBAdapter";

private static final String WORDS_CREATE =
    "create table words (_id primary key autoincrement,word text,"
    + "T1 text,T2 text,T3 text,T4 text,T5 text,"
    + "recent integer,rating integer);";

private static final String CATEGORIES_CREATE =
    "create table categories (_id integer primary key autoincrement,category text unique);";

private static final String BELONGS_CREATE =  
 "create table belongs (_id integer primary key autoincrement,word text,category text,"
    + "foreign key (word) references words (word),foreign key (category) references categories (category));";

private static final String USER_CREATE =
 "create table user (uid text unique, pw text, admin integer);";

private static final String STATISTICS_CREATE =   
    "create table statistics (_id integer primary key  autoincrement, uid text unique, gamesPlayed integer, wordsCorrect integer, "
    +"wordsWrong integer, pointTotal integer, wins integer, losses integer, "
    +"foreign key (uid) references user (uid));";

private static final String SETTINGS_CREATE =
    "create table settings (_id integer primary key autoincrement, uid text, sid text, "
    +"type text, duration integer, ptsToWin integer, "
    +"foreign key (uid) references user (uid));"
    ;


//**************************************************************
//*********************  Function Constants ********************
//**************************************************************

private final Context context; 
private DatabaseHelper DBHelper;
public static SQLiteDatabase db;
private int wordCount = 100;

//********************  Object 
public DBAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
 DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(WORDS_CREATE);
        db.execSQL(CATEGORIES_CREATE);
        db.execSQL(BELONGS_CREATE);
        db.execSQL(USER_CREATE);
        db.execSQL(STATISTICS_CREATE);
        db.execSQL(SETTINGS_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                          int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
              + " to "
              + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + WORDS_TABLE);
        onCreate(db);
    }
}    

//****************************************************
//********  Database Functions  **********************
//****************************************************

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

我的插入代码:

public long enterUser(ContentValues content)
{
 long result = db.insert(USER_TABLE, null, content);
 return result;
}

任何想法都会非常感激。谢谢!

1 个答案:

答案 0 :(得分:1)

DATABASE_NAME =“禁忌”需要为“taboo.db”