日期:2014-05-16  浏览次数:20468 次

在cmd窗口中查询android的sqlite3数据库表之步骤

本文主要是写了一个android程序对sqlite3中数据库的employee表的插入、删除的操作,然后在cmd窗口中用sql命令查询employee表的操作过程.

1、第一步:首先把程序写好.

1.1、创建一个MainActivity类,代码如下:

package com.example.datastorege.action;

import com.example.datastorege.R;
import com.example.datastorege.R.layout;
import com.example.datastorege.R.menu;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
1.2、创建一个SQLiteOpenHelperImpl类,这个类是继承SQLiteOpenHelper类的,SQLiteOpenHelperImpl主要作用把创建的DB、DB的版本号通过SQLiteOpenHelper来实现,同时好在EmployeeDao获取db对象,以便操作sqlite3中的employee表.中代码如下:
package com.example.datastorege.dao;

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

public class SQLiteOpenHelperImpl extends SQLiteOpenHelper{

	private static final String TAG="EmployeeDaoJunit";
	private static final int DB_VERSION=1;
	private static final String DB_NAME="testDb.db";
	private static final String SQL_TABLE_NAME="create table employee(id INTEGER PRIMARY KEY,empName varchar(30),sex int);";
	
	public SQLiteOpenHelperImpl(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(SQL_TABLE_NAME);		
		Log.i(TAG, "execSQL successful");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
	

}

1.3、创建操作sqlite3的dao类---EmployeeDao,这个类主要是操作sqlite3中testDb.db数据库名,代码如下:

package com.example.datastorege.dao;

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

import com.example.datastorege.model.Employee;

public class EmployeeDao {
	
	private SQLiteOpenHelperImpl helper; 
	private SQLiteDatabase db;
	
	public EmployeeDao(Context context){
		helper=new SQLiteOpenHelperImpl(context);
	}

	public void insert(Employee emp){
		db=helper.getWritableDatabase();
		db.execSQL("insert into employee values(?,?,?)", new Object[]{emp.getId(),emp.getEmpName(),emp.getSex()});
	}
	
	public void update(Employee emp){
		db=helper.getWritableDatabase();
		db.execSQL("update employee set empName=?,sex=? where id=?",new Object[]{emp.getEmpName(),emp.getSex(),emp.getId()});
	}
	
	public void delete(int id){
		db=helper.getWritableDatabase();
		db.execSQL("delete from employee where id=?", new Object[]{id});
	}
	
	public Employee queryBeanById(int id){
		Employee emp=null;
		db=helper.getWritableDatabase();
		Cursor cursor=db.rawQuery("",new String[]{});
		if(cursor.moveToNext()){
			emp=new Employee();
			emp.setEmpName(cursor.getString(cursor.getColumnIndex("empName")));
			emp.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
			return emp;
		}	
		return null;
	}
	
	
}

1.4、创建一个实体bean.

package com.example.datastorege.model;

public class Employee {
	
	private int id;
	private String empName;
	private int sex;
	
	public Employee(){};
	
	public Employee(int id,String empName,int sex){
		this.id=id;
		this.empName=empName;
		this.sex=sex;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public int getSex() {
		return sex;
	}
	public void setSex