0

这是我的主要Activity工作,我正在做我的所有工作。我在创建SQLite数据库时遇到错误。

private final String DB_NAME = "pocketCloudlets";
private final String TABLE_NAME_1 = "Members";
long count = 0;
static final String userTable="user";

static final String userName = "User";
static final String userID = "_id";
static final String age ="age";
static final String gender = "gender";
static final String password = "password";
static final String email = "email";

// Creating Table for User Record  

protected static final String table1=(" CREATE TABLE IF NOT EXISTS " 
    + userTable + " ("
    + userID + " INTEGER PRIMARY KEY NOT NULL, "
    + userName + " VARCHAR ,"
    + age + " VARCHAR ,"
    + gender + " VARCHAR )");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.main);

        ArrayList<String> results = new ArrayList<String>();
        SQLiteDatabase sampleDB = null;

        try {      
            sampleDB = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
            sampleDB.execSQL(table1);

            sampleDB.execSQL("INSERT INTO " +
                userTable +
                " Values ('13','dawdaa','4','female');");

            Cursor c = sampleDB.rawQuery("SELECT _id,userName, age, gender FROM " +
                userTable + " ", null);
        } catch (Throwable t) {
            // completed by the editor of this question
            // the 'catch' part of the try-catch structure was NOT present
        }
4

3 回答 3

2

我想这在您第一次开始活动时有效。现在您已经插入了一行,_id = 13现在,每次您再次执行此操作时,您都会一遍又一遍地创建相同的 id。

由于您已指定_id为表的主键,因此这是不允许的 - 主键必须是唯一的。

于 2012-09-05T19:41:48.667 回答
0

尝试将 userID 列设置为 Integer Primary Key AutoIncrement。我也很确定你不能在 sqlite 中使用 VARCHAR - 你需要使用 TEXT 来代替。查看此链接以供参考:http ://www.sqlite.org/datatype3.html

于 2012-09-05T19:32:45.067 回答
0
Cursor c = sampleDB.rawQuery("SELECT _id,userName, age, gender FROM " +
    userTable + " ", null);

您在表中没有userName列,而是使用该user列创建了表。

改用:

Cursor c = sampleDB.rawQuery("SELECT _id, user, age, gender FROM " + userTable, null);
于 2012-09-05T19:10:25.433 回答