为了账号安全,请及时绑定邮箱和手机立即绑定

为什么主键没有自增长?​

代码跟老师的一样啊,为什么主键没有自增长?都是NULL

SQLiteDatabase db = openOrCreateDatabase("user.db",MODE_PRIVATE,null);
db.execSQL("create table if not exists usertb (_id integer not null primary key autoincrement,name text not null,age integer,sex text not null)");
db.execSQL("insert into usertb(name,age,sex) values('张三',18,'女')");
db.execSQL("insert into usertb(name,age,sex) values('李四',19,'女')");
db.execSQL("insert into usertb(name,age,sex) values('王五',20,'男')");
Cursor c = db.rawQuery("select * from usertb",null);
if(c!=null){
    while(c.moveToNext()){
        Log.i("info","_id:"+c.getInt(c.getColumnIndex("_id")));
        Log.i("info","name:"+c.getString(c.getColumnIndex("name")));
        Log.i("info","age:"+c.getInt(c.getColumnIndex("age")));
        Log.i("info","sex:"+c.getString(c.getColumnIndex("sex")));
        Log.i("info","__________________________________");
    }
    c.close();
}
db.close();

打出来的结果ID全是0,navicat 里面看到ID全是null。。


06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: name:张三
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: age:18
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: name:李四
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: age:19
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.405 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:王五
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:20
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:男
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:张三
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:18
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:李四
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:19
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:王五
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:20
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:男
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:张三
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:18
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:李四
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:19
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:王五
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:20
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:男
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:张三
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:18
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:李四
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:19
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: sex:女
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: _id:0
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: name:王五
06-12 05:53:30.406 26973-26973/com.geekband.sqllitedemo01 I/info: age:20
06-12 05:53:30.407 26973-26973/com.geekband.sqllitedemo01 I/info: sex:男
06-12 05:53:30.407 26973-26973/com.geekband.sqllitedemo01 I/info: __________________________________
06-12 05:53:30.493 26973-27023/com.geekband.sqllitedemo01 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED:

正在回答

1 回答

尝试把create table if not exists usertb (_id integer 中的usertb后面的空格删掉

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android攻城狮的第二门课(第3季)
  • 参与学习       74912    人
  • 解答问题       420    个

手势识别、数据存储和4大组件,让你牢牢掌握之前的基础知识点

进入课程

为什么主键没有自增长?​

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信