Android操作Sqlite

1.编辑main.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <Button android:id="@+id/btnCreateDataBase"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnCreateDatabase"/>
    
    <Button android:id="@+id/btnUpdateDataBase"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnUpdateDatabase"/>
    <Button android:id="@+id/btnInsert"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnInsert"/>
          
    <Button android:id="@+id/btnUpdate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnUpdate"/>
             
    <Button android:id="@+id/btnQuery"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnQuery"/>
</LinearLayout>

2.新建一个DataBaseHelper.java的文件

package wei.cao.Android.db;

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

//DataBaseHelper作为一个访问SQLite的助手类,提供两个方面的功能
//第一,getReadableDatabase(),getWriteableDataBase()可获得SQLiteDatabase对象,通过该对象可以对数据库进行操作
//第二,提供了onCreate()和onUpgrade()两个回调函数,允许我们在创建或升级数据库的时候,进行我们自己的操作

public class DataBaseHelper extends SQLiteOpenHelper {

	private static final int VERSION=1;
	
	//在SQLiteOpenHelper的子类中必须指定该构造函数
	public DataBaseHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}
    
	public DataBaseHelper(Context context,String name)
	{
		this(context,name,VERSION);
	}
	public DataBaseHelper(Context context,String name,int version)
	{
		this(context,name,null,VERSION);
	}
	
	//改函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabase对象的时候,才会调用这个方法
	//(仅仅生成DataBaseHelper对象是不会调用的,只有使用getReadableDatabase()或getWriteableDataBase()才会调用)
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
		System.out.println("create a database");
		
		//execSQL函数用于执行SQL语句
		db.execSQL("create table user(id int,name varchar(20))");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		System.out.println("update a database");
	}

}


3.编辑SqliteActivity.java中的内容

package wei.cao.Android;

import wei.cao.Android.db.DataBaseHelper;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SqliteActivity extends Activity {
    /** Called when the activity is first created. */
   
    
    Button btnCreateDataBase=null;
    Button btnUpdateDataBase=null;
    Button btnInsert=null;
    Button btnUpdate=null;
    Button btnQuery=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btnCreateDataBase=(Button)this.findViewById(R.id.btnCreateDataBase);
        btnUpdateDataBase=(Button)this.findViewById(R.id.btnUpdateDataBase);
        btnInsert=(Button)this.findViewById(R.id.btnInsert);
        btnUpdate=(Button)this.findViewById(R.id.btnUpdate);
        btnQuery=(Button)this.findViewById(R.id.btnQuery);
        
        btnCreateDataBase.setOnClickListener(new OnClickListener(){

   public void onClick(View v) {
    // TODO Auto-generated method stub
    
    //创建一个DatabaseHelper对象
    DataBaseHelper DBHelper=new DataBaseHelper(SqliteActivity.this, "test.db");
    //只有调用了DatabaseHelper对象的getReadableDatabase()或getWriteableDataBase()方法才会创建表
    //调用onCreate
    SQLiteDatabase db=DBHelper.getReadableDatabase();
   }});
        btnUpdateDataBase.setOnClickListener(new OnClickListener(){

      public void onClick(View v) {
       // TODO Auto-generated method stub
          //创建一个DatabaseHelper对象(版本改变了)
    DataBaseHelper DBHelper=new DataBaseHelper(SqliteActivity.this, "test.db",  2);
    //由于版本发生了改变将调用onUpgrade方法
    SQLiteDatabase db=DBHelper.getReadableDatabase();
      }});
        btnInsert.setOnClickListener(new OnClickListener(){

      public void onClick(View v) {
       // TODO Auto-generated method stub
       //生成ContentValues对象
       ContentValues values=new ContentValues();
       //向改对象中插入键值对时,其中键是列名,值是希望插入到这一列的值,值的类型必须跟数据库中列的类型匹配
       values.put("id", 1);
       values.put("name", "zhangsan");
       DataBaseHelper DBHelper=new DataBaseHelper(SqliteActivity.this, "test.db");
       SQLiteDatabase db=DBHelper.getWritableDatabase();
       //调用Insert方法,就可以将数据插入到数据库当中
       //null表示当ContentValues对象为空时,插入的数据为一条null的数据
       db.insert("user", null, values);
       
      }});
        
        btnUpdate.setOnClickListener(new OnClickListener(){

      public void onClick(View v) {
       // TODO Auto-generated method stub
          //生成ContentValues对象
       ContentValues values=new ContentValues();
       //得到一个可写的SQLiteDatabase对象
       DataBaseHelper DBHelper=new DataBaseHelper(SqliteActivity.this, "test.db");
       SQLiteDatabase db=DBHelper.getWritableDatabase();
       
       values.put("name", "zhangsanfeng");
          //第一个参数是要更新的表名
             //第二个参数是一个ContentValues对象
       //第三个参数是where子句(占位符,有几个,后面的string就有几个)
       db.update("user",values, "id=?" ,new String[]{"1"});
       
      }});
        btnQuery.setOnClickListener(new OnClickListener(){

      public void onClick(View v) {
       // TODO Auto-generated method stub
       DataBaseHelper DBHelper=new DataBaseHelper(SqliteActivity.this, "test.db");
       SQLiteDatabase db=DBHelper.getReadableDatabase();
       Cursor cursor=db.query("user",new String[]{"name","id"},"id=?",new String[]{"1"},null,null, null);
       while(cursor.moveToNext())
       {
        String name=cursor.getString(cursor.getColumnIndex("name"));
        System.out.println("query result------>"+name);
       }
      }});
    }
}

4.执行结果


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值