android SQLite使用

阅读更多
package zhou.demo.sql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * 提供的功能
 * 1:getReadableDatabase(),gerWritableDatabase()可以获取SQLiteDatabase对象,通过该对象进行相关操作
 * 2:提供onCreate()和onUpgrade两个回调函数,允许我们在创建和升级数据库时,进行
 * @author Administrator
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper{
 private static final int VERSION = 1;
 /**
  * 该函数为继承SQLiteOpenHelper必须有的一个函数
  * @param context
  * @param name
  * @param factory
  * @param version
  */
 public DatabaseHelper(Context context, String name, CursorFactory factory,
   int version) {
  super(context, name, factory, version);
  
 }
 public DatabaseHelper(Context context, String name){
  this(context,name,VERSION);
 }
 public DatabaseHelper(Context context, String name, int version){
  this(context,name,null,version);
 }
 public void onCreate(SQLiteDatabase db) {
  System.out.println("数据库已经创建");
  db.execSQL("create table user(id int,name varchar(20))");
  
 }
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  System.out.println("数据库已经更新");
 }

}

 

 

 

 操作数据库必须继承SQLiteOpenHelper不然会很麻烦,

package zhou.demo;

import zhou.demo.sql.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 SQLiteDemo extends Activity {
    private Button createButton;
    @SuppressWarnings("unused")
 private Button updateButton;
    private Button insertButton;
    private Button updateRecordButton;
    private Button queryButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //进行控件的关联
        createButton = (Button)this.findViewById(R.id.button1);
        updateButton = (Button)this.findViewById(R.id.button4);
        insertButton = (Button)this.findViewById(R.id.button3);
        updateRecordButton = (Button)this.findViewById(R.id.button2);
        queryButton = (Button)this.findViewById(R.id.button5);
        
        
        //进行操作的关联
        createButton.setOnClickListener(new OnClickListener(){
   @SuppressWarnings("unused")
   public void onClick(View v) {
    //创建数据库对象
    DatabaseHelper dbHelper = new DatabaseHelper(SQLiteDemo.this,"zhou_table");
    //调用才会真正的创建数据库
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    
   }
        });
        
        //更新数据库的版本
        updateRecordButton.setOnClickListener(new OnClickListener(){

   @SuppressWarnings("unused")
   @Override
   public void onClick(View v) {
    DatabaseHelper dbHelper = new DatabaseHelper(SQLiteDemo.this,"zhou_table",2);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    
   }
         
        });
        //插入监听
        insertButton.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    ContentValues values = new ContentValues();
    values.put("id", 123);
    values.put("name", "zhouzhou");
    DatabaseHelper dbHelper = new DatabaseHelper(SQLiteDemo.this,"zhou_table");
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    db.insert("zhou_table", null, values);
   }
         
        });
        
        //更新数据超早
        updateRecordButton.setOnClickListener(new OnClickListener(){
   public void onClick(View v) {
    DatabaseHelper dbHelper = new DatabaseHelper(SQLiteDemo.this,"zhou_table");
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    ContentValues values = new ContentValues();
    values.put("name", "周利军");
    //把String数组的一个参数传给id(值值相互对应)
    db.update("zhou_table", values, "id=?", new String[]{"1"});
    
   }
         
        });
        
        //查询
        queryButton.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    DatabaseHelper dbHelper = new DatabaseHelper(SQLiteDemo.this,"zhou_table");
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cursor = db.query("zhou_table", new String[]{"id","name"}, "id=?", new String[]{"1",""}, null, null, null);
    while(cursor.moveToNext()){
     String name = cursor.getString(cursor.getColumnIndex("name"));
     System.out.println("查到的---->>>" + name);
    }
   }
         
        });
    }
}

 实现对数据库的操作必须做到这两步:

//创建数据库对象
    DatabaseHelper dbHelper = new DatabaseHelper(SQLiteDemo.this,"zhou_table");
    //调用才会真正的创建数据库
    SQLiteDatabase db = dbHelper.getWritableDatabase();

你可能感兴趣的:(Android,SQLite,SQL,OS)