Android 台灣中文網
標題: 有關SQL資料庫的程式碼 抓bug到快哭出來 [打印本頁]
作者: stu60001 時間: 2014-10-12 03:28
標題: 有關SQL資料庫的程式碼 抓bug到快哭出來
本帖最後由 stu60001 於 2014-10-12 03:30 編輯
public class MainActivity extends Activity {
private SQLiteDatabase db;
private MyDBHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn1);
}
public void btn1OnClick(View v) {
dbHelper = new MyDBHelper(this);
db = dbHelper.getWritableDatabase();
TextView output = (TextView) findViewById(R.id.txv1);
output.setText("資料庫是否開啟:" + db.isOpen() + "
資料版本:" + db.getVersion());
}
}
public class MyDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MyBooks";
private static final int DATABASE_VERSION = 1;
public MyDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE titles (_id"
+ "integer primary key autoincrement, "
+ "title text no null, price real no null)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS titles");
onCreate(db);
}
}
下面是上面的程式碼,在手機上不能執行,就是不知道哪裡出問題...
gistb586cadd362e0c0c1290-56346c005a33379faac92ba798f7a851a751b401.tar.gz
(971 Bytes, 下載次數: 0)