解决android: SQLiteDatabaseLockedException: database is locked

上网百度了很多,有用信号量、synchronize、lock的等等

我选用了单例模式,

因为同时只能有一个SQLiteOpenHelper访问sqlite数据库,如果同时有两个SQLiteOpenHelper访问sqlite则报错:SQLiteDatabaseLockedException: database is locked

看我的方法:

package DAO;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	public static Integer VERSION = 1;
	public static String DBNAME = "spray_db_1";// 定义数据库名
	public static String APPVERSION = "v1.0.0";
	private static DBHelper instance;

	private DBHelper(Context context) {
		super(context, DBNAME, null, VERSION);
		// TODO 自动生成的构造函数存根
	}

	public static DBHelper getInstance(Context context) {
		if (instance == null) {
			instance = new DBHelper(context);
		}

		return instance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO 自动生成的方法存根
		// db.execSQL("create  table   city_weather (city  varchar(50),date_ref date,time_ref time,weather_ref  varchar(1000))");
		// db.execSQL("create table update_table (update_count  int ,update_date  date)");
		// ////不可有中文逗号
		// 创建表,保存当前登录的用户是谁
		db.execSQL("create  table   user (id  varchar(15)  ,login_time  date, face_path varchar(50))");
		// /创建表,保存发送或收到消息后成为好友的好友信息
		db.execSQL("create  table friend(user_id varchar(15),nickname varchar(20),sex varchar(2),age int,city varchar(20),status  int)");
		// db.execSQL("create  table friend(user_id varchar(15),nickname varchar(20))");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO 自动生成的方法存根
		db.execSQL("alter  table   user   add   extends_column  varchar(10)");
		db.execSQL("alter  table   friend   add   extends_column  varchar(10)");

	}

}
package DAO;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.Button;



public class DAO {

	private static DBHelper helper;
	private static SQLiteDatabase db;
	
	

	public DAO(Context context) {
		//helper = new DBHelper(context);
		helper=DBHelper.getInstance(context);///单例模式
	}

	/////55555555555查询我的所有好友,并返回
	public ArrayList getAllMyFriend(){
		ArrayList  friendList = new ArrayList();
		//db=helper.getWritableDatabase();
		db=helper.getReadableDatabase();
		Cursor cursor=db.rawQuery("select  nickname  from   friend ",null);
		while(cursor.moveToNext()){
			friendList.add(cursor.getString(0));
			Log.i("----DAO.getAllMyFriend--------", cursor.getString(0));
		}
		if(friendList.size()==0){
			friendList.add("暂无好友");
			Log.i("----DAO.getAllMyFriend----暂无好友----", "暂无好友");
		}
		db.close();/////千万别忘了关闭!!!!!!!!
		return friendList;
	}

	
}//DAO
public class ContactsFragment extends Fragment {
	//private Button btn1;
	
	DAO dao = new DAO(getContext());
     ............

			ArrayList  myFriendList = new ArrayList();
			myFriendList=dao.getAllMyFriend();//查询我的所有好友,
			int size= myFriendList.size();
		//	int size= LoginActivity.myFriendList.size();
			String[]  myFriendArray  = new String[size];
			for(int i=0;i
简单说明了在这三个类中的定义和调用

这样就行了



你可能感兴趣的:(Android)