base de datos sqlite de operación android y experiencia

Escribir este artículo se debe principalmente a que hay demasiadas y demasiado complicadas operaciones en sqlite en Internet, y en muchos casos no se pueden usar bien en sus propios proyectos. La estructura no está clara. Escribí una operación que es adecuada para personas que Acabo de contactarme. método.


Recientemente, al usar Android, necesito guardar algunos datos. Al principio usé las preferencias. Después, hubo más cosas que guardar. Descubrí que el uso de preferencias obviamente no podía cumplir con los requisitos, y descubrí que el código se volvía un poco complicado si usé esto. Comencé a aprender a usar una base de datos sqlite. Al principio pensé que era una base de datos. Los servidores mysql o sqlserver habituales son iguales. Todos son simples, pero más tarde, cuando realmente lo uso, descubrí que el Las dificultades son una a una, pero sigue siendo continua. Después de intentar resolver las dificultades paso a paso, descubrí que es una buena opción ver algunos videos de enseñanza en Internet cuando estoy aprendiendo Android cuando realmente no puedo encontrar ideas. Es bueno recomendar un video aquí. Lo aprendí de este video. http://www.tudou.com/programs/view/2qH5Am_3MsM/


Escriba algunos preparativos en la base de datos operativa de Android.

En primer lugar, configure las variables de entorno de adb, porque es demasiado problemático ir al directorio adb cada vez que se inicia. Los siguientes son los pasos específicos. Por supuesto, también se pueden utilizar otros archivos. Estoy acostumbrado a cambiar esto . Después del cambio, puede obtenerlo. Entra en vigor.

1. sudo gedit   / etc / profile
2. Agregue las siguientes dos oraciones al archivo abierto arriba  export ANDROID_HOME = / home / sdk file path export PATH = $ PATH: $ ANDROID_HOME / platform-tools


3. Reinicie la computadora y listo. !

Después de configurar adb, será mejor que demos acceso a la base de datos en el teléfono móvil, generalmente en / data / data / package name / database. Después de ingresar con adb shell, su obtiene el permiso de root del teléfono móvil y luego da el permiso chmod.

Si desea leer el archivo de la base de datos, use el comando sqlite3 database file. La base de datos que contiene puede ejecutar sqlite3 directamente en el shell adb, pero no puedo abrir la base de datos sqlite3 en el shell adb de acuerdo con lo que encontré en línea, diciendo que no se encuentra el comando, el archivo que debo cargar Está todo aprobado, no hay forma, solo poner los archivos de la base de datos en todas partes en el explorador de archivos de ddms en eclipse y luego ejecutar la base de datos sqlite3 en la terminal de Linux para ver.


También tenga en cuenta que al escribir sentencias sql, debe prestar atención a "seleccionar * de" + TABLE_NAME en el desde y las comillas para dejar espacios; de lo contrario, se vincularán entre sí.

El siguiente es un conocimiento sobre el que hablar, la adición, eliminación y otras operaciones de SQLite son bastante simples, el problema es la operación de consulta, generalmente use Cursor para guardar los datos de la consulta, al principio no presté mucha atención a esto es un tipo de puntero, apuntando a la base de datos Cuando escribí por primera vez, escribí la operación de cierre de la base de datos antes de la operación Cursor. Es decir, la base de datos se cierra primero y luego se opera el objeto Cursor. Esto hará que el puntero Cursor sea nulo Estaba destinado a tomar una taza durante mucho tiempo. . .



Aquí hay algunos ejemplos de operaciones de base de datos SQLite, que ayudarán a algunas personas que todavía están confundidas y, al mismo tiempo, las ayudarán a revisarlas más adelante.


SQLiteHelper.java (clase auxiliar de base de datos)

public class SQLiteHelper extends SQLiteOpenHelper{
	
	private static final String DATABASE_NAME="fpp.db";
	private static final int  DATABASE_VERSION=17;//更改版本后数据库将重新创建
	private static final String TABLE_NAME="test";
	
	/**
     * 在SQLiteOpenHelper的子类当中,必须有这个构造函数
     * @param context     当前的Activity
     * @param name        表的名字(而不是数据库的名字,这个类是用来操作数据库的)
     * @param factory      用来在查询数据库的时候返回Cursor的子类,传空值
     * @param version      当前的数据库的版本,整数且为递增的数
     */
    public SQLitedata(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);//继承父类
        // TODO Auto-generated constructor stub
    }
/**
     * 该函数是在第一次创建数据库时执行,只有当其调用getreadabledatebase()
     * 或者getwrittleabledatebase()而且是第一创建数据库是才会执行该函数
     */

    public void onCreate(SQLiteDatabase db)
    {
     
        // TODO Auto-generated method stub
        String sql = "CREATE TABLE " + TABLE_NAME + "("
        		+ "id INTEGER,"
        		+ "nid VARCHAR(11),"
        		+ "sid CHAR(1),"
        		+ "type INTEGER,"
        		+ "stime DATETIME,"
        		+ "locate_main VARCHAR(45),"
        		+ "locate_detail VARCHAR(45),"
        		+ "state INTEGER"
        		+ ")";
        db.execSQL(sql);
        Log.e("create","数据库创建成功");
    }
/**
*数据库更新函数,当数据库更新时会执行此函数
*/

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
    	String sql = "DROP TABLE IF EXISTS " + TABLE_NAME; 
    	db.execSQL(sql);
    	this.onCreate(db);
        // TODO Auto-generated method stub
        System.out.println("数据库已经更新");
        /**
         * 在此添加更新数据库是要执行的操作
         */
    }
  
}


MyOperator.java (clase de operación de base de datos)

public class MyOperator {
	
	private static final String TABLE_NAME = "test";//要操作的数据表的名称
	private SQLiteDatabase db=null;	//数据库操作
	
	//构造函数
	public MyOperator(SQLiteDatabase db)
	{
		this.db=db;
	}
	
//	//插入操作
//	public void insert(int id,String nid,String sid,int type,
//			String stime,String etime,String desc,String locate_main,String locate_detail,int state)
//	{
//		String sql = "INSERT INTO " + TABLE_NAME + " (id,nid,sid,type,stime,etime,desc,locate_main,locate_detail,state)"
//				+ " VALUES(?,?,?,?,?,?,?,?,?,?)";
//		Object args[]=new Object[]{id,nid,sid,type,stime,etime,desc,locate_main,locate_detail,state};
//		this.db.execSQL(sql, args);
//		this.db.close();
//	}
	//插入重载操作
	public void insert(int id,int state)
	{
		String sql = "INSERT INTO " + TABLE_NAME + " (id,state)" +" VALUES(?,?)";
		Object args[]=new Object[]{id,state};
		this.db.execSQL(sql, args);
		this.db.close();
	}
	
	
	//更新操作
	public void update(int id,int state)
	{
		String sql = "UPDATE " + TABLE_NAME + " SET state=? WHERE id=?";
		Object args[]=new Object[]{state,id};
		this.db.execSQL(sql, args);
		this.db.close();
	}
	
	//删除操作,删除
	public void delete(int id)
	{
		String sql = "DELETE FROM " + TABLE_NAME +" WHERE id=?";
		Object args[]=new Object[]{id};
		this.db.execSQL(sql, args);
		this.db.close();
	}
	
	//查询操作,查询表中所有的记录返回列表
	public List<String> find()
	{
		List<String> all = new ArrayList<String>();	//此时只是String
		String sql = "SELECT * FROM " + TABLE_NAME;
		Cursor result = this.db.rawQuery(sql, null); 	//执行查询语句
		for(result.moveToFirst();!result.isAfterLast();result.moveToNext()	)	//采用循环的方式查询数据
		{
			all.add(result.getInt(0)+","+result.getString(1)+","+result.getString(2)+","+result.getInt(3)+","
					+result.getString(4)+","+result.getString(5)+","+result.getString(6)+","+result.getString(7)+","
					+result.getString(8));
		} 
		this.db.close();
		return all;
	}
	
	//查询操作虫重载函数,返回指定ID的列表
	public int getstatebyID(int id)
	{
		int num=-1;//错误状态-1
		List<String> all = new ArrayList<String>();	//此时只是String
		String sql = "SELECT state FROM " + TABLE_NAME + " where id=?" ;
		String args[] = new String[]{String.valueOf(id)};
		Cursor result = this.db.rawQuery(sql, args);
		for(result.moveToFirst();!result.isAfterLast();result.moveToNext()	)
		{
			num=result.getInt(0);
		}
		
		Log.e("database", "图片状态state"+ String.valueOf(num));
		this.db.close();
		return num;
	}
	
	//判断插入数据的ID是否已经存在数据库中。
	public boolean check_same(int id)
	{
		String sql="SELECT id from " + TABLE_NAME + " where id = ?";
		String args[] =new String[]{String.valueOf(id)};
		Cursor result=this.db.rawQuery(sql,args);
		Log.e("database", "the sql has been excuate");
		
		Log.e("database","the hang count" + String.valueOf(result.getCount()));
		
		if(result.getCount()==0)//判断得到的返回数据是否为空
		{
			Log.e("database", "return false and not exist the same result" + String.valueOf(result.getCount()));
			this.db.close();
			return false;
		}
		else
		{
			Log.e("database", "return true and exist the same result"+ String.valueOf(result.getCount()));
			this.db.close();
			return true;
		}
	}
}

Cualquier actividad en un proyecto (clase de actividad, operación para abrir la base de datos ya está en él)

public class MainActivity extends Activity {
	private static LinkedList<Map<String, Object>> mListItems;
	private PullToRefreshListView pullToRefreshListView;
	private ProblemController problemController = ProblemController.getInstance();
	
	private SQLiteOpenHelper helper =null;
	private MyOperator mytab=null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.helper=new SQLitedata(this);//数据库操作辅助类
        setPullToRefreshView();
    }

    
    @Override
    public boolean onCreateOptionsMenu(Menu _menu){
    	super.onCreateOptionsMenu(_menu);
    	_menu.add(0, 0, 0, "设置");
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem _item){
    	switch (_item.getItemId()) {
			case 0:{
				Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
				startActivity(intent);
				break;
			}
		}
    	return true;
    }
    
    private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
        	if(listToShowData(problemController.getNewProblems())){
        		
        	}else {
				Message message = new Message();
				handler.sendMessage(message);
        	}
        	return mStrings;
        }

        @Override
        protected void onPostExecute(String[] result) {
            pullToRefreshListView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }

    private String[] mStrings = {};

	/**
	 * @param _newslist 需要显示的消息列表
	 */
	private boolean listToShowData(LinkedList<Problem> _problems) {
		if(_problems != null){
			mListItems.clear();
			for (Problem news : _problems) {
				//将数据插入数据库进行初始化
				//这里需要一个判断重复的操作,如果数据的id已经在数据库中就不需要进行插入数据
				
				
				mytab = new MyOperator(helper.getWritableDatabase());
				Log.e("database", "start check if id exists and insert the id,the id is "+String.valueOf(news.getID()));
				if(!mytab.check_same(news.getID()))
				{
					Log.e("database", "the id is not exist,insert the new id now.....");
					mytab = new MyOperator(helper.getWritableDatabase());
					mytab.insert(news.getID(), 1);
					Log.e("database", "insert finished");
				}
				
				
				Map<String, Object> tmpMap = new HashMap<String, Object>();	//用来储存日志名称的Map
				tmpMap.put("id", news.getID());
				tmpMap.put("describe", "模块:" + news.getSid() + "出现故障!");
				tmpMap.put("time", news.getsTime());
				tmpMap.put("img", R.drawable.icon_important);
				
				Log.e("database", "start read database");
				
				//读取数据库判断这个事件的状态显示对应的图标,1表示默认状态问号,2表示已察看,3表示事件已经完成勾
				mytab = new MyOperator(helper.getWritableDatabase());
				int state = mytab.getstatebyID(news.getID());
				switch(state)
				{
				case 1:tmpMap.put("state_img", R.drawable.icon_question);break;
				case 2:tmpMap.put("state_img", R.drawable.icon_process);break;
				case 3:tmpMap.put("state_img", R.drawable.icon_correct);break;
				default:tmpMap.put("state_img", R.drawable.icon_correct);
				}
				mListItems.add(tmpMap);
				Log.e(news.toString(), news.toString());
			}
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * @param 对下拉刷新控件进行设置
	 */
	private void setPullToRefreshView(){
		mListItems = new LinkedList<Map<String,Object>>();
		pullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pullToRefreshListView1);
		pullToRefreshListView.setOnRefreshListener(new OnRefreshListener() {
		    public void onRefresh() {
		        new GetDataTask().execute();	//拉数据的线程开启
		    }
		});
		pullToRefreshListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2,
					long arg3) {
				Log.e("Pull", String.valueOf(arg2));
				ShareData.showProblem = problemController.getOldProblems().get(arg2 - 1);
				Intent intent = new Intent(getApplicationContext(), DetailsActivity.class);
				
				
				//设置新的图片,现在用数据库的方法,所以这个操作就不需要了,到时候统一读取图片
//				ImageView tempImage=(ImageView)arg1.findViewById(R.id.imageView2);
//				tempImage.setImageResource(R.drawable.icon_correct);
				//把状态存入数据库,判断图片状态,如果为1则说明没有被访问过,改变为2
				mytab = new MyOperator(helper.getWritableDatabase());
				if(mytab.getstatebyID(ShareData.showProblem.getID())==1)
				{
					mytab = new MyOperator(helper.getWritableDatabase());
					mytab.update(ShareData.showProblem.getID(), 2);
				}
				
//将故障ID传入到选项界面,以便与判断哪个页面选了哪些选项。				
				int id=ShareData.showProblem.getID();
				Bundle bd=new Bundle();
				bd.putInt("id", id);
				intent.putExtra("ID", bd);
				
				
				startActivity(intent);
//传入到了另一个界面
			}
		});
		pullToRefreshListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				Log.e("PullLong", String.valueOf(arg2));
				return true;
			}
		});
		SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), mListItems, R.layout.layout_listitem, 
				new String[]{"id", "img", "describe", "time" ,"state_img"}, 
				new int[]{R.id.title_TV, R.id.imageView1, R.id.content_TV, R.id.date_TV, R.id.imageView2});
        pullToRefreshListView.setAdapter(adapter);
        
	}
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message message) {
			Toast.makeText(getApplicationContext(), "网络状况出现问题!", Toast.LENGTH_LONG).show();
		}
	};
}



Supongo que te gusta

Origin blog.csdn.net/u012457196/article/details/38408747
Recomendado
Clasificación