SQLite使用例子

 
package  com.peidw.demo;

import  java.io.FileNotFoundException;

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

/**
 * 数据库操作
 * 
@author  peidw 2009-04-06
 *
 
*/
public   class  DatabaseHelper  extends  SQLiteOpenHelper{
    
private  Context ctx  =   null ;
    
    
public  DatabaseHelper(Context context, String name, CursorFactory factory,  int  version) {
        
super (context, name, factory, version);

    }
    

    @Override
    
public   void  onCreate(SQLiteDatabase arg0) {
        
    }
    @Override
    
public   void  onUpgrade(SQLiteDatabase arg0,  int  arg1,  int  arg2) {
        
    }

    

}
package  com.peidw.demo;

import  java.util. * ;
import  android.app. * ;
import  android.content.Context;
import  android.database.Cursor;
import  android.database.sqlite.SQLiteDatabase;
import  android.database.sqlite.SQLiteOpenHelper;
import  android.database.sqlite.SQLiteDatabase.CursorFactory;
import  android.os. * ;
import  android.util. * ;

/**
 * 操作数据库例子
 * 
@author  peidw 2009-04-19
 *
 
*/
public   class  ConnectionProvider {
    
    
private   static   SQLiteDatabase db = null ;
    
    
    
public   ConnectionProvider(Context ctx) {
        
if  (db == null ){
            Log.v(
" ConnectionProvider " , " ctx= " + ctx.toString());
            DatabaseHelper dbHelper
= new  DatabaseHelper(ctx, " StudDB " null , 1 );
            db
=  dbHelper.getWritableDatabase();
        }
    }
    
    
public  SQLiteDatabase getConnection (){
        
return  db;
    }
    
    
public   void  closeConnection (){
        db.close();
    }
    
/**
     * 判断表是否存在
     * 
@param  tablename
     * 
@return
     
*/
    
public   boolean  isTableExits(String tablename){
        
boolean  result = false ; // 表示不存在
        String str = " select count(*) xcount  from sqlite_master where table=' " + tablename + " ' " ;
        Cursor c 
=  db.rawQuery(str, null );
        
int  xcount = c.getColumnIndex( " xcount " );
        
if (xcount != 0 ){
            result
= true // 表存在
        }
        
return  result;
    }
}

在程序中操作数所的代码片段

 ConnectionProvider cp
= new  ConnectionProvider( this .getBaseContext());
        
//  if(cp.isTableExits(TABLE_NAME)){ // 表已存在
             createTable(cp);
            queryTable(cp);
        
//  }else{ // 表不存在
        
//      createTable();
        
//  }



public   void  createTable( ConnectionProvider cp){
        

        SQLiteDatabase db
=  cp.getConnection();
        db.execSQL(
" CREATE TABLE IF NOT EXISTS  "   +  TABLE_NAME  +   "  ( "
                
+   " stud_no "   +   "  TEXT, "
                
+   " stud_name "   +   "  TEXT "
                
+   " ); " );
    
        String sql_1 
=   " insert into  " +  TABLE_NAME  + "  (stud_no, stud_name) values('S777', 'Lily Chang'); " ;
        String sql_2 
=   " insert into  "   +  TABLE_NAME  + "  (stud_no, stud_name) values('S888', 'Linda Lin'); " ;
        String sql_3 
=   " insert into  "   +  TABLE_NAME  + "  (stud_no, stud_name) values('S999', 'Bruce Wang'); " ;
        db.execSQL(sql_1); db.execSQL(sql_2); db.execSQL(sql_3);
    }
    
public   void  queryTable(ConnectionProvider cp){
        String str
= " select * from  " + TABLE_NAME;
        Cursor c 
= cp.getConnection().rawQuery(str, null );
        Log.v(
" loginactive " , " 查询记录 " );
        Log.v(
" loginactive " ,String.valueOf(c.getCount()) );
        
if (c != null ){
            c.moveToFirst();
            
while ( ! c.isLast()){
                Log.v(
" loginactive " , " outp. " );
                String stud_no
= c.getString( 0 );
                String stud_name
= c.getString( 1 );
                System.out.println(stud_no
+ " --------------- " + stud_name);
                Log.v(
" loginactive " ,stud_no + " --------------- " + stud_name);
                c.moveToNext();
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值