06-08 21:39:20.053: E/PhonePolicy(4043): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
06-08 21:39:32.253: W/dalvikvm(4043): threadid=1: thread exiting with uncaught exception (group=0x40a851f8)
06-08 21:39:32.263: E/AndroidRuntime(4043): FATAL EXCEPTION: main
06-08 21:39:32.263: E/AndroidRuntime(4043): android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:307)
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:168)
06-08 21:39:32.263: E/AndroidRuntime(4043): at com.example.android_sqliterdemo.MainActivity$UpdateListener.onClick(MainActivity.java:111)
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.view.View.performClick(View.java:3511)
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.view.View$PerformClick.run(View.java:14110)
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.os.Handler.handleCallback(Handler.java:605)
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.os.Looper.loop(Looper.java:137)
06-08 21:39:32.263: E/AndroidRuntime(4043): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-08 21:39:32.263: E/AndroidRuntime(4043): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 21:39:32.263: E/AndroidRuntime(4043): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 21:39:32.263: E/AndroidRuntime(4043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-08 21:39:32.263: E/AndroidRuntime(4043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-08 21:39:32.263: E/AndroidRuntime(4043): at dalvik.system.NativeStart.main(Native Method)
06-08 21:40:58.283: E/PhonePolicy(4099): Could not preload class for phone policy: com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): Couldn't open test_m_db1 for writing (will try read-only):
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:307)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:168)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at com.example.android_sqliterdemo.MainActivity$QueryListener.onClick(MainActivity.java:133)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.view.View.performClick(View.java:3511)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.view.View$PerformClick.run(View.java:14110)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.os.Handler.handleCallback(Handler.java:605)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.os.Looper.loop(Looper.java:137)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-08 21:40:59.603: E/SQLiteOpenHelper(4099): at dalvik.system.NativeStart.main(Native Method)
06-08 21:40:59.613: W/dalvikvm(4099): threadid=1: thread exiting with uncaught exception (group=0x40a851f8)
06-08 21:40:59.613: E/AndroidRuntime(4099): FATAL EXCEPTION: main
06-08 21:40:59.613: E/AndroidRuntime(4099): android.database.sqlite.SQLiteException: Can't upgrade read-only database from version 2 to 1: /data/data/com.example.android_sqliterdemo/databases/test_m_db1
06-08 21:40:59.613: E/AndroidRuntime(4099): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:244)
06-08 21:40:59.613: E/AndroidRuntime(4099): at com.example.android_sqliterdemo.MainActivity$QueryListener.onClick(MainActivity.java:133)
06-08 21:40:59.613: E/AndroidRuntime(4099): at android.view.View.performClick(View.java:3511)
06-08 21:40:59.613: E/AndroidRuntime(4099): at android.view.View$PerformClick.run(View.java:14110)
06-08 21:40:59.613: E/AndroidRuntime(4099): at android.os.Handler.handleCallback(Handler.java:605)
06-08 21:40:59.613: E/AndroidRuntime(4099): at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 21:40:59.613: E/AndroidRuntime(4099): at android.os.Looper.loop(Looper.java:137)
06-08 21:40:59.613: E/AndroidRuntime(4099): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-08 21:40:59.613: E/AndroidRuntime(4099): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 21:40:59.613: E/AndroidRuntime(4099): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 21:40:59.613: E/AndroidRuntime(4099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-08 21:40:59.613: E/AndroidRuntime(4099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-08 21:40:59.613: E/AndroidRuntime(4099): at dalvik.system.NativeStart.main(Native Method) java
下面是源代碼,但願高手們能幫助我檢查一下: android
Activity中的代碼: sql
package com.example.android_sqliterdemo; import com.sql.db.DatabaseHelper; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { // 聲明各個組件 private Button createdb; private Button updatedb; private Button insert; private Button update; private Button query; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getPart(); getListener(); } // 經過ID得到各個組件 public void getPart() { this.createdb = (Button) findViewById(R.id.createdb); this.updatedb = (Button) findViewById(R.id.updatedb); this.insert = (Button) findViewById(R.id.insert); this.update = (Button) findViewById(R.id.update); this.query = (Button) findViewById(R.id.query); } // 爲控件綁定監聽事件 public void getListener() { this.createdb.setOnClickListener(new CreatebdListener()); this.updatedb.setOnClickListener(new UpdateBDListener()); this.insert.setOnClickListener(new InsertListener()); this.update.setOnClickListener(new UpdateListener()); this.query.setOnClickListener(new QueryListener()); } // 建立各個按鈕的監聽事件 // 建立數據庫 public class CreatebdListener implements OnClickListener { @Override public void onClick(View v) { // 實例化DatabaseHelper對象 DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_m_db1"); // 得到SQLiteDatabase對象 SQLiteDatabase db = dbHelper.getReadableDatabase(); } } // 更新數據庫 public class UpdateBDListener implements OnClickListener { @Override public void onClick(View v) { // 實例化DatabaseHelper對象 DatabaseHelper db = new DatabaseHelper(MainActivity.this, "test_m_db1", 2); // 獲得SQLiteDatabase對象 SQLiteDatabase database = db.getReadableDatabase(); } } // 插入數據 public class InsertListener implements OnClickListener { @Override public void onClick(View v) { // 生成ContentValues對象 ContentValues values = new ContentValues(); // 將該對象中應該插入的鍵值對,其中間是列名,值是但願插入這一列中值 values.put("id", 1); values.put("name", "zhangsan"); DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_m_db1",2); // 經過writer方式得到數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); // 向數據庫中插入數據 表格名稱、是否爲空、插入的值 db.insert("users", null, values); } } // 修改數據庫中的表格 public class UpdateListener implements OnClickListener { @Override public void onClick(View v) { // 得到ContetValues對象 ContentValues values = new ContentValues(); // 向values中添加鍵值對 values.put("name", "李四"); // 實例化DatabaseHelper對象 DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_m_db1"); // 使用writer方法得到數據庫 SQLiteDatabase db = dbHelper.getWritableDatabase(); // 修改數據庫中表格中數據 // 1.要更新的表格的名稱 // 2.是一個ContentValues對象 // 3.where語句 ?是佔位符 // 佔位符處得值 db.update("users", values, "id=?", new String[] { "1" }); } } // 查詢數據庫中表格的數據 public class QueryListener implements OnClickListener { @Override public void onClick(View v) { // 實例化DatabaseHelper對象 DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this, "test_m_db1"); // 使用reader的方法得到SQLiteDatabse數據庫 SQLiteDatabase db = dbHelper.getReadableDatabase(); //執行查尋操做 Cursor cursor=db.query("users", new String[]{"id" , "name"},"id=?",new String[]{"1"}, null, null, null); while(cursor.moveToNext()){ String name=cursor.getString(cursor.getColumnIndex("name")); System.out.println("query---->"+name); } } } }
SQLiteDataBaseHelper子類代碼「 數據庫
package com.sql.db; import android.content.Context; import android.database.DatabaseErrorHandler; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; /** * 這是一個操做數據庫的助手類, * 功能:繼承到了父類的構造方法和onCreate,onUpgrade方法 * 繼承父類中getReadableDatabase和getWritableDatabase中操做 * @author Administrator * */ public class DatabaseHelper extends SQLiteOpenHelper { private static final int VERSION=1; //在SQLiteOpenHelper的子類中,必須包含的構造方法,做用: //1.context:activity 2.name 數據庫的名稱 3.cursorFactory:不知道 //4.version:數據庫的版本,正數且遞增 public DatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } public DatabaseHelper(Context context, String name, CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, name, factory, version, errorHandler); // TODO Auto-generated constructor stub } // 自定義兩個參數的構造函數 public DatabaseHelper(Context context, String name) { this(context, name, VERSION); } public DatabaseHelper(Context context, String name, int version) { this(context, name, null, version); } public DatabaseHelper(Context context, String name, CursorFactory factory) { this(context, name, null, VERSION); } // 該函數是在第一次建立數據庫的時候才能用到這個方法 @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("create a Database"); db.execSQL("create table users(id int,name varchar(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub // 更新數據庫,增刪改的方法都得調用這個函數 System.out.println("update a Database"); } }