黑马程序员技术交流社区
标题:
求大神解答啊,弄了好久都没有弄明白
[打印本页]
作者:
潘成旺
时间:
2015-8-21 23:25
标题:
求大神解答啊,弄了好久都没有弄明白
写了一段Android代码创建sqlite数据库,数据库是创建成功了,但是代码执行到创建表的时候就崩溃了,这是为啥呀,求解惑!求告知
创建了一个类继承SQLiteOpenHelper
public class MyDBSQLite extends SQLiteOpenHelper {
private static final String tag = "create";
public MyDBSQLite(Context context) {
super(context, "itheima.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v(tag, "数据库被创建了");
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20)");
Log.v(tag, "表被创建了");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
复制代码
Activity中的代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void add(View view){
MyDBSQLite helper = new MyDBSQLite(this);
SQLiteDatabase db = helper.getWritableDatabase();
Random random = new Random();
db.execSQL("insert into info (name,phone) values (?,?)",new Object[]{"zhangsan"+random.nextInt(100),"110-"+random.nextInt(100)});
db.close();
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2