SQLite使用实例

前端之家收集整理的这篇文章主要介绍了SQLite使用实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

完整代码下载:http://download.csdn.net/detail/zhangpengyu321/5990803



DBHelp.java

package com.gem.android.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.database.sqlite.sqliteDatabase.CursorFactory;
import android.util.Log;

public class DBHelper extends sqliteOpenHelper {
	
	private sqliteDatabase db;   
	
	private static final int DB_VERSION=2;
	
	private static final String DB_NAME="Test.db";
	
	private static final String TBL_NAME="test";
	
	private static final String CREATE_TBL="create table test(_id integer primary key autoincrement,name text,url text,desc text)";
	
	
	public DBHelper(Context context) {
		// TODO Auto-generated constructor stub
//		db.openOrCreateDatabase("/data/data/com.gem.android.manager/databases/Test.db",null);
		super(context,DB_NAME,null,DB_VERSION);
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.database.sqlite.sqliteOpenHelper#onCreate(android.database.sqlite.sqliteDatabase)
	 */
	@Override
	public void onCreate(sqliteDatabase db) {
		// TODO Auto-generated method stub
//		db=this.createDatabase();
		this.db=db;
		db.execsql(CREATE_TBL);
	}
	// 创建数据库,注意这里要指定全路径
//    private sqliteDatabase createDatabase(){
//    	return sqliteDatabase.openOrCreateDatabase("/data/data/com.gem.android.manager/databases/Test.db",null);
//    }
	
	public void insert(ContentValues values){
		//拿到当前DB的链接
		db=this.getWritableDatabase();
		db.insert(TBL_NAME,values);
		db.close();
	}
	
	public Cursor query(){
		db=this.getWritableDatabase();
		Cursor cursor=db.query(TBL_NAME,null);
		return cursor;
	}
	
	public void delete(int id){
		db=this.getWritableDatabase();
		db.delete(TBL_NAME,"_id=?",new String[]{String.valueOf(id)});
		db.close();
	}
	
	public void close(){
		if(db!=null){
			db.close();
		}
	}
	/*
	 * (non-Javadoc)
	 * @see android.database.sqlite.sqliteOpenHelper#onUpgrade(android.database.sqlite.sqliteDatabase,int,int)
	 */
	@Override
	public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
		// TODO Auto-generated method stub

	}

}


MainActivity.java

package com.gem.android.manager;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.gem.android.util.DBHelper;

public class MainActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
	
	private EditText edittext1,edittext2,edittext3;
	
	private Button button1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.setTitle("添加收藏信息");
        edittext1=(EditText) this.findViewById(R.id.editText1);
        edittext2=(EditText) this.findViewById(R.id.editText2);
        edittext3=(EditText) this.findViewById(R.id.editText3);
        
        button1=(Button) this.findViewById(R.id.button1);
        button1.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String name=edittext1.getText().toString();
		String url=edittext2.getText().toString();
		String desc=edittext3.getText().toString();
		
		ContentValues values=new ContentValues();
		values.put("name",name);
		values.put("url",url);
		values.put("desc",desc);
		
		DBHelper helper=new DBHelper(this.getApplicationContext());
		helper.insert(values);
		
		Intent intent=new Intent(MainActivity.this,QueryActivity.class);
		this.startActivity(intent);
	}
}


QueryActivity.java
package com.gem.android.manager;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;

import com.gem.android.util.DBHelper;

public class QueryActivity extends ListActivity implements OnItemClickListener,OnClickListener{
	
	private long temp;
	private DBHelper helper =new DBHelper(this);
	
	private SimpleCursorAdapter adapter;
	
	 @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle("浏览收藏信息");
        Cursor cursor=helper.query();
        String[] from={"_id","name","url","desc"};
    	int[] to={R.id.text1,R.id.text2,R.id.text3,R.id.text4};
        SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.row,cursor,from,to);
        ListView listView=this.getListView();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
	    helper.close();
     }
	
	
	 /*
	  * (non-Javadoc)
	  * @see android.widget.AdapterView.OnItemClickListener#onItemClick(android.widget.AdapterView,android.view.View,long)
	  */
	@Override
	public void onItemClick(AdapterView<?> arg0,View arg1,int arg2,long arg3) {
		// TODO Auto-generated method stub
		temp=arg3;
		AlertDialog.Builder builder=new AlertDialog.Builder(this);
		builder.setMessage("真的要删除该记录吗?");
		builder.setPositiveButton("确定",this);
		builder.setNegativeButton("取消",null);
		builder.create().show();
	}

	@Override
	public void onClick(DialogInterface dialog,int which) {
		// TODO Auto-generated method stub
		helper.delete((int)temp);
		Cursor cursor=helper.query();
		String[] from={"_id","desc"};
	    int[] to={R.id.text1,R.id.text4};
	    SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,to);
	    ListView listView=this.getListView();
	    listView.setAdapter(adapter);
	}
}
原文链接:https://www.f2er.com/sqlite/201104.html

猜你在找的Sqlite相关文章