Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “主键应该是唯一的SQLite”错误_Android_Sqlite - Fatal编程技术网

Android “主键应该是唯一的SQLite”错误

Android “主键应该是唯一的SQLite”错误,android,sqlite,Android,Sqlite,这是我做所有工作的主要活动。我在创建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 =

这是我做所有工作的主要活动。我在创建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
        }
表中没有userName列,而是使用user列创建了表

改用:

Cursor c = sampleDB.rawQuery("SELECT _id, user, age, gender FROM " + userTable, null);

尝试将userID列设置为Integer主键自动递增。我还非常确定不能在sqlite中使用VARCHAR,而是需要使用文本。查看此链接以供参考:

我想这在您第一次开始活动时就起作用了。现在您已经插入了一行_id=13,现在,每次您再次这样做,您都会一遍又一遍地创建相同的id


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

显示您通过发布日志而得到的错误。使用AUTOINCREMENT不需要此错误,并且VARCHAR对SQLite有效。请告诉我+1正如biegleux指出的,select查询也被破坏了。
Cursor c = sampleDB.rawQuery("SELECT _id, user, age, gender FROM " + userTable, null);