SQLiteDatebase--- android数据库操作出现错误了

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)

下面是源代码,希望高手们能帮助我检查一下:

Activity中的代码:

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");
	}

}

 

你可能感兴趣的:(sqlite)