黑马程序员技术交流社区

标题: 求大神解答啊,弄了好久都没有弄明白 [打印本页]

作者: 潘成旺    时间: 2015-8-21 23:25
标题: 求大神解答啊,弄了好久都没有弄明白
写了一段Android代码创建sqlite数据库,数据库是创建成功了,但是代码执行到创建表的时候就崩溃了,这是为啥呀,求解惑!求告知
创建了一个类继承SQLiteOpenHelper
  1. public class MyDBSQLite extends SQLiteOpenHelper {
  2.         private static final String tag = "create";
  3.         public MyDBSQLite(Context context) {
  4.                 super(context, "itheima.db", null, 1);
  5.         }

  6.         @Override
  7.         public void onCreate(SQLiteDatabase db) {
  8.                 Log.v(tag, "数据库被创建了");
  9.                 db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20),phone varchar(20)");
  10.                 Log.v(tag, "表被创建了");
  11.         }

  12.         @Override
  13.         public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
  14.                 // TODO Auto-generated method stub

  15.         }

  16. }
复制代码


Activity中的代码:

  1. public class MainActivity extends Activity {

  2.         @Override
  3.         protected void onCreate(Bundle savedInstanceState) {
  4.                 super.onCreate(savedInstanceState);
  5.                 setContentView(R.layout.activity_main);
  6.         }
  7.         public void add(View view){
  8.                 MyDBSQLite helper = new MyDBSQLite(this);
  9.                 SQLiteDatabase db = helper.getWritableDatabase();
  10.                 Random random = new Random();
  11.                 db.execSQL("insert into info (name,phone) values (?,?)",new Object[]{"zhangsan"+random.nextInt(100),"110-"+random.nextInt(100)});
  12.                 db.close();
  13.         }
  14. }
复制代码










欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2