android数据库原始写法

PersonDBOpenHelper.java

package com.demo.introductiontothedb;

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


/**
 * 1 .写一个类 继承 SQLiteOpenHelper 帮助创建数据库 版本的控制
 * @author Administrator
 *
 */
public class PersonDBOpenHelper extends SQLiteOpenHelper {

	
	/**
	 * 数据库创建帮助类的构造方法 
	 * @param context
	 */
	public PersonDBOpenHelper(Context context) {
		//name 数据库文件的名称
		//factory 访问数据库一个数据库的游标工厂
		// version 数据库的版本
		super(context, "person.db", null, 1);
	}
	/**
	 * 当数据库第一次被创建的是 调用的方法.
	 * 适合做数据库表结构的初始化
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("create table personInfo (id integer primary key autoincrement, name varchar(20), phone varchar(20),address varchar(50)) ");
	}

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

	}

}
PersonDao.java

package com.demo.introductiontothedb.dao;

import com.demo.introductiontothedb.PersonDBOpenHelper;

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


public class PersonDao {
	//增删改查
	//javaweb 1.加载jdbc驱动.连接 2.准备sql 3.查询
	
	private PersonDBOpenHelper helper;

	//任何人使用 dao 都要传递一个上下文
	public PersonDao(Context context) {
		helper = new PersonDBOpenHelper(context);
	}
	/**
	 * 添加一条记录
	 */
	public void add(String name,String phone,String addresss){
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("insert into personInfo (name,phone,address) values (?,?,?)", new Object[]{name,phone,addresss});
		db.close();
	}
	

	
	/**
	 * 根据名字查找一条记录
	 */
	public int find(String name){
		int id = -1;
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select id from personInfo where name=?", new String[]{name});
		if(cursor.moveToFirst()){
			id = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return id;
	}
	
	/**
	 * 删除一条记录
	 */
	public void delete(int id){
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("delete from personInfo where id=?", new Object[]{id});
		db.close();
	}
	
	/**
	 * 更改一条记录
	 */
	public void update(String name,String phone,int id){
		SQLiteDatabase db = helper.getWritableDatabase();
		db.execSQL("update personInfo set name=?,phone=? where id=?", new Object[]{name,phone,id});
		db.close();
	}
	

	
}

TestPerson.java

package com.demo.introductiontothedb.test;

import com.demo.introductiontothedb.dao.PersonDao;
import android.test.AndroidTestCase;

public class TestPerson extends AndroidTestCase {
	private PersonDao dao;
	@Override
	protected void setUp() throws Exception {
		dao = new PersonDao(getContext());
		super.setUp();
	}
	
	public void testAdd(){
		dao.add("AV", "123", "岛国");
	}
	
	public void testDelete(){
		int id = dao.find("刘亦菲");
		dao.delete(id);
	}

	public void testUpdate(){
		dao.update("麻生希", "545565", 8);
	}	
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.introductiontothedb"
    android:versionCode="1"
    android:versionName="1.0" >

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:label="Tests for My App"
        android:targetPackage="com.demo.introductiontothedb" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <!-- 在application节点下添加 使用的测试library -->
        <uses-library android:name="android.test.runner" />

        <activity
            android:name="com.demo.introductiontothedb.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



你可能感兴趣的:(sqlite数据库)