MyOpenHelper mySqlite = new MyOpenHelper(MainActivity.this); SQLiteDatabase db = mySqlite.getWritableDatabase(); // 实例数据库 /*===================================================*/package com.sqlite20150721_02; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; public class MyOpenHelper extends SQLiteOpenHelper { public static final String TAG = "MyOpenHelper"; public MyOpenHelper(Context context) { super(context, "test_sqlite07271816", null, 1); // TODO Auto-generated constructor stub Log.i(TAG, "MyOpenHelper -> MyOpenHelper()"); } @Override //http://byandby.iteye.com/blog/835580 public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub //Log.i(TAG, "MyOpenHelper ->onCreate()"); // TODO Auto-generated method stub //execSQL用于执行SQL语句 db.execSQL("create table user(id integer primary key autoincrement,name varchar(20))"); Log.i(TAG, "创建数据库时执行的:MyOpenHelper ->onCreate()"); /*db.execSQL("create table user(id int,name varchar(20))"); String sql = "create table s_member(s_id integer(5) primary key, s_username varchar(50),s_userword varchar(50))"; db.execSQL(sql); Log.i(TAG, "创建数据库时执行的:MyOpenHelper ->onCreate()");*/ /* try { db.execSQL("create table teacher(id int,name varchar(20))"); db.endTransaction(); } catch (Exception e) { e.printStackTrace(); } System.out.println("create a database");*/ } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub Log.i(TAG, "MyOpenHelper ->onUpgrade()"); } }
//大神们 请看码 那个onCreate方法里面的表就是创建不出来,但是数据库可以创建的;就是当一个数据库不存在的时候执行吧,这个库是可以创建出来,但是在onCreate()方法里面的创建表语句无法创建出数据表,但是我把这个语句放到命令行去执行就可以创建出来表,求大神们指点,,谢谢!