Chinaunix首页 | 论坛 | 博客
  • 博客访问: 460569
  • 博文数量: 155
  • 博客积分: 2954
  • 博客等级: 少校
  • 技术积分: 1000
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-12 22:00
文章分类

全部博文(155)

文章存档

2014年(2)

2013年(5)

2012年(10)

2011年(33)

2010年(105)

我的朋友

分类: 嵌入式

2010-08-23 16:10:22

Java代码 复制代码
  1. package com.Aina.Android;   
  2.   
  3. import android.content.Context;   
  4. import android.database.sqlite.SQLiteDatabase;   
  5. import android.database.sqlite.SQLiteOpenHelper;   
  6. import android.database.sqlite.SQLiteDatabase.CursorFactory;   
  7.   
  8. /**  
  9.  * com.Aina.Android Pro_SQLiteDatabase  
  10.  *   
  11.  * @author Aina.huang E-mail: 674023920@qq.com  
  12.  * @version 创建时间:2010 Jun 29, 2010 9:57:21 AM 类说明  
  13.  */  
  14. public class DatabaseHelper extends SQLiteOpenHelper {   
  15.   
  16.     private Context mContext = null;   
  17.     private String str = "";   
  18.     private String table = "";   
  19.   
  20.     public DatabaseHelper(Context context, String name, CursorFactory factory,   
  21.             int version, String str, String table) {   
  22.         super(context, name, factory, version);   
  23.         this.mContext = context;   
  24.         this.str = str;   
  25.         this.table = table;   
  26.     }   
  27.   
  28.     @Override  
  29.     public void onCreate(SQLiteDatabase db) {   
  30.         // TODO Auto-generated method stub   
  31.         db.execSQL(str);   
  32.     }   
  33.   
  34.     @Override  
  35.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {   
  36.         db.execSQL("DROP TABLE "+table);   
  37.         this.onCreate(db);   
  38.   
  39.     }   
  40. }  


Java代码 复制代码
  1. package com.Aina.Android;   
  2.   
  3. import android.content.ContentValues;   
  4. import android.content.Context;   
  5. import android.database.Cursor;   
  6. import android.database.sqlite.SQLiteDatabase;   
  7.   
  8. /**  
  9.  * com.Aina.Android Pro_SQLiteDatabase  
  10.  *   
  11.  * @author Aina.huang E-mail: 674023920@qq.com  
  12.  * @version 创建时间:2010 Jun 29, 2010 10:11:57 AM 类说明  
  13.  */  
  14. public class MyDataBaseAdapter {   
  15.   
  16.     private Context mContext = null;   
  17.     private SQLiteDatabase mSQLiteDatabase = null;   
  18.     private DatabaseHelper dh = null;   
  19.     private static final String DB_NAME = "Text.db";   
  20.     private static final int DB_VERSION = 1;   
  21.     private static final String TABLE_NAME = "table1";   
  22.     private static final String COLUMN_ID = "_id";   
  23.     public static final String COLUMN_NAME = "name";   
  24.     public static final String COLUMN_DATA = "data";   
  25.     private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME   
  26.             + " (" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME   
  27.             + " TEXT," + COLUMN_DATA + " INTEGER NOT NULL)";   
  28.     public MyDataBaseAdapter(Context context){   
  29.         mContext = context;   
  30.     }   
  31.     /**  
  32.      * 打开数据库  
  33.      */  
  34.     public void open(){   
  35.         dh = new DatabaseHelper(mContext,DB_NAME,null,DB_VERSION,CREATE_TABLE,TABLE_NAME);   
  36.         mSQLiteDatabase = dh.getWritableDatabase();   
  37.     }   
  38.     /**  
  39.      * 关闭数据库  
  40.      */  
  41.     public void close(){   
  42.         dh.close();   
  43.     }   
  44.     /**  
  45.      * 插入  
  46.      */  
  47.     public void insert(){   
  48.         try{   
  49.             ContentValues cv = new ContentValues();   
  50.             cv.put(COLUMN_NAME, "张三");   
  51.             cv.put(COLUMN_DATA, 10);   
  52.             mSQLiteDatabase.insert(TABLE_NAME, null, cv);   
  53.         }catch(Exception ex){   
  54.             ex.printStackTrace();   
  55.         }   
  56.     }   
  57.     /**  
  58.      * 修改  
  59.      */  
  60.     public void update(){   
  61.         try{   
  62.             String sql = "UPDATE "+TABLE_NAME+" SET "+COLUMN_NAME+" ='李四' WHERE  _id=1";   
  63.             mSQLiteDatabase.execSQL(sql);   
  64.         }catch(Exception ex){   
  65.             ex.printStackTrace();   
  66.         }   
  67.     }   
  68.     /**  
  69.      * 修改  
  70.      */  
  71.     public void delete(){   
  72.         try{   
  73.             mSQLiteDatabase.delete(TABLE_NAME, "_id=2"null);   
  74.         }catch(Exception ex){   
  75.             ex.printStackTrace();   
  76.         }   
  77.     }   
  78.     /**  
  79.      * 查询  
  80.      */  
  81.     public Cursor select(){   
  82.         Cursor cursor = null;   
  83.         try{   
  84.             String sql = "SELECT * FROM "+TABLE_NAME;   
  85.             cursor = mSQLiteDatabase.rawQuery(sql, null);   
  86.             return cursor;   
  87.         }catch(Exception ex){   
  88.             ex.printStackTrace();   
  89.             return null;   
  90.         }   
  91.     }   
  92. }  


Java代码 复制代码
  1. package com.Aina.Android;   
  2.   
  3. import android.app.Activity;   
  4. import android.database.Cursor;   
  5. import android.graphics.Color;   
  6. import android.os.Bundle;   
  7. import android.view.KeyEvent;   
  8. import android.widget.LinearLayout;   
  9. import android.widget.ListAdapter;   
  10. import android.widget.SimpleCursorAdapter;   
  11. import android.widget.LinearLayout.LayoutParams;   
  12. import android.widget.ListView;   
  13.   
  14. public class Test extends Activity {   
  15.     /** Called when the activity is first created. */  
  16.     private MyDataBaseAdapter mdba = null;   
  17.     private LinearLayout ll = null;   
  18.     private ListView lv = null;   
  19.   
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {   
  22.         super.onCreate(savedInstanceState);        
  23.         ll = new LinearLayout(this);   
  24.         ll.setOrientation(LinearLayout.VERTICAL);   
  25.         ll.setBackgroundColor(Color.BLACK);   
  26.         ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,   
  27.                 LayoutParams.WRAP_CONTENT));   
  28.         lv = new ListView(this);   
  29.         lv.setBackgroundColor(Color.BLACK);   
  30.         ll.addView(lv, new LayoutParams(LayoutParams.FILL_PARENT,   
  31.                 LayoutParams.WRAP_CONTENT));   
  32.         setContentView(ll);   
  33.         mdba = new MyDataBaseAdapter(this);   
  34.         mdba.open();   
  35.         this.show();   
  36.     }   
  37.   
  38.     @Override  
  39.     protected void onPause() {   
  40.         super.onPause();   
  41.         mdba.close();   
  42.     }   
  43.   
  44.     @Override  
  45.     public boolean onKeyDown(int keyCode, KeyEvent event) {   
  46.         switch (keyCode) {   
  47.         case KeyEvent.KEYCODE_DPAD_LEFT:   
  48.             mdba.insert();   
  49.             break;   
  50.         case KeyEvent.KEYCODE_DPAD_RIGHT:   
  51.             mdba.delete();   
  52.             break;   
  53.         case KeyEvent.KEYCODE_DPAD_UP:   
  54.             mdba.update();   
  55.             break;   
  56.         }   
  57.         this.show();   
  58.         return super.onKeyDown(keyCode, event);   
  59.     }   
  60.   
  61.     private void show() {   
  62.         Cursor cursor = mdba.select();   
  63.         if (cursor != null) {   
  64.             ListAdapter adapter = new SimpleCursorAdapter(this,   
  65.                     android.R.layout.simple_list_item_2, cursor, new String[] {   
  66.                             MyDataBaseAdapter.COLUMN_NAME,   
  67.                             MyDataBaseAdapter.COLUMN_DATA }, new int[] {   
  68.                             android.R.id.text1, android.R.id.text2 });   
  69.             lv.setAdapter(adapter);   
  70.         }   
  71.     }   
  72. }  
阅读(1130) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~