android 入门 006(sqlite增删改查)

android 入门 006(sqlite增删改查)

package cn.rfvip.feb_14_2_sqlite;

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

public class MyOpenHelper extends SQLiteOpenHelper {

    public MyOpenHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO 自动生成的构造函数存根
        
        
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        // TODO 自动生成的方法存根
        System.out.println("创建数据库");
        db.execSQL("CREATE TABLE people (_id integer PRIMARY KEY AUTOINCREMENT,name char(10),salary char(20),phone integer(20)) ");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO 自动生成的方法存根
        System.out.println("更新数据库");
    }

}

 

package cn.rfvip.feb_14_2_sqlite.UnitTest;

import cn.rfvip.feb_14_2_sqlite.MyOpenHelper;
import android.content.res.ObbInfo;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.test.AndroidTestCase;

public class UnitTest extends AndroidTestCase 
{
 private MyOpenHelper myOpenHelper;
private SQLiteDatabase db;

//测试之前调用
@Override
protected void setUp() throws Exception {
    // TODO 自动生成的方法存根
    super.setUp();
    myOpenHelper=new MyOpenHelper(getContext(), "people.db", null, 2);
    db = myOpenHelper.getWritableDatabase();
}

//测试之后调用
    @Override
    protected void tearDown() throws Exception 
    {
        // TODO 自动生成的方法存根
        super.tearDown();
        db.close();
    }

    public  void Test() 
    {
        myOpenHelper=new MyOpenHelper(getContext(), "people.db", null, 2);
        SQLiteDatabase db=myOpenHelper.getWritableDatabase();
    }

    public void Insert()
    {
        db.execSQL("insert into people(name,salary,phone)values(?,?,?)",new Object[]{"李纯访1","15000",180178});
        db.execSQL("insert into people(name,salary,phone)values(?,?,?)",new Object[]{"李纯访2","15000",180178});
        db.execSQL("insert into people(name,salary,phone)values(?,?,?)",new Object[]{"李纯访3","15000",180178});
        db.execSQL("insert into people(name,salary,phone)values(?,?,?)",new Object[]{"李纯访4","15000",180178});
        db.execSQL("insert into people(name,salary,phone)values(?,?,?)",new Object[]{"李纯访5","15000",180178});
    }
    
    public void Delete()
    {
        db.execSQL("delete from people where name= ?",new Object[]{"李纯访"});
    }
    public void Update()
    {
        db.execSQL("update people set phone=? where name= ?",new Object[]{153333,"李纯访"});
    }
    public void Select()
    {
         Cursor cursor=db.rawQuery("select name,salary from people", null);
         while (cursor.moveToNext())
         {
            String name=cursor.getString(cursor.getColumnIndex("name"));
            String salary=cursor.getString(cursor.getColumnIndex("salary"));
            System.out.println(name + ";" + salary);
            
        }
    }
}

 

google 封装的增删改查

// google 封装的方法
    public void InsertApi() {
        ContentValues values = new ContentValues();
        values.put("name", "李度");
        values.put("salary", "20000");
        values.put("phone", "15000");
        Long numberrow = db.insert("people", null, values);
        System.out.println("插入的当前行数:" + numberrow);
    }

    public void DeleteApi() {
        int numberrow = db.delete("people", "name=? and _id= ?", new String[] {
                "李纯访1", "2" });
        System.out.println("删除的行数:" + numberrow);
    }

    public void UpdateApi()
    {
         ContentValues values=new ContentValues();
         values.put("name", "lidu");
         int numberrow= db.update("people", values, "name= ? and _id= ?",new String[]{"李纯访2", "3" } );
         System.out.println("更新的行数:"+numberrow);
    }
    //columns 为null查询所有 new String[]{"name","salary"}
    public void SelectApi() 
    {
        Cursor cursor=db.query("people", null,  null, null, null, null, null);
        while (cursor.moveToNext()) 
        {
        String name=cursor.getString(cursor.getColumnIndex("name"));    
        String salary=cursor.getString(cursor.getColumnIndex("salary"));
        String phone=cursor.getString(cursor.getColumnIndex("phone"));
        System.out.println("姓名为:" + name +";工资为:" + salary +";电话为:" + phone );
        }
    }

 

你可能感兴趣的:(android 入门 006(sqlite增删改查))