zoukankan      html  css  js  c++  java
  • 在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(int sex) {
    		this.sex = sex;
    	}
    	
    	@Override
    	public String toString() {
    		return this.id+"--"+this.empName+"--"+this.sex;
    	}
    	
    
    }
    


    1.5、创建一个android的junit测试类:

    package com.example.datastorege;
    
    import com.example.datastorege.dao.EmployeeDao;
    import com.example.datastorege.model.Employee;
    
    import android.test.AndroidTestCase;
    import android.util.Log;
    
    public class EmployeeDaoJunit extends AndroidTestCase{
    
    	private static final String TAG="EmployeeDaoJunit";
    	
    	public void add(){
    		EmployeeDao employeeDao=new EmployeeDao(this.getContext());
    		employeeDao.insert(new Employee(2,"tangfq",1));
    		Log.i(TAG, "EmployeeDao--->insert");
    	}
    	
    	public void delete(){
    		EmployeeDao employeeDao=new EmployeeDao(this.getContext());
    		employeeDao.delete(1);
    		Log.i(TAG, "EmployeeDao--->delete");
    	}
    	
    }
    

    以上就是要写的程序.查询和修改在测试类添加就行了.操作和新增、删除类似.

    2、第二步:运行上面写的程序及操作sqlite3数据库.

    2.1、运行EmployeeDaoJunit ,在运行它之前,要先运行sqlite3.exe,运行起后才执行EmployeeDaoJunit ,当junit运行绿色时,则证明写正确了.此时,在运行中输入"cmd"--->adb shell---->ls---->cd data---->cd data--->(在eclipse中打开DDMS Pespective,点击"File Explorer"查看,data--->data--->你的项目名包路径,如我的是com.example.datastorege)--->databases--->你的数据库名,我的数据库名为testDb.db--->sqlite3 testDb.db--->select id,empName,sex from employee;---->显示操作的employee表的结果.

    操作的步骤是上面所描述的,如果有什么疑问或错误之处,敬请留言!谢谢了!




  • 相关阅读:
    PostgreSQL 慢查询SQL语句跟踪
    unity WheelCollider调整
    C# 事件event
    C# 委托
    C4D导入fbx到Unity设置
    3DMAX导出fbx到unity设置
    unity 单面模型shader
    对数平均数
    GameObject:activeInHierarchy、activeSelf Behaviour:enabled
    unity Time.deltaTime
  • 原文地址:https://www.cnblogs.com/pangblog/p/3320207.html
Copyright © 2011-2022 走看看