package com.Aina.Android;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.database.sqlite.SQLiteDatabase.CursorFactory;/** * com.Aina.Android Pro_SQLiteDatabase *  * @author Aina.huang E-mail: 674023920@qq.com * @version 创建时间:2010 Jun 29, 2010 9:57:21 AM 类说明 */public class DatabaseHelper extends SQLiteOpenHelper {private Context mContext = null;private String str = "";private String table = "";public DatabaseHelper(Context context, String name, CursorFactory factory,int version, String str, String table) {super(context, name, factory, version);this.mContext = context;this.str = str;this.table = table;}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL(str);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {db.execSQL("DROP TABLE "+table);this.onCreate(db);}}


package com.Aina.Android;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;/** * com.Aina.Android Pro_SQLiteDatabase *  * @author Aina.huang E-mail: 674023920@qq.com * @version 创建时间:2010 Jun 29, 2010 10:11:57 AM 类说明 */public class MyDataBaseAdapter {private Context mContext = null;private SQLiteDatabase mSQLiteDatabase = null;private DatabaseHelper dh = null;private static final String DB_NAME = "Text.db";private static final int DB_VERSION = 1;private static final String TABLE_NAME = "table1";private static final String COLUMN_ID = "_id";public static final String COLUMN_NAME = "name";public static final String COLUMN_DATA = "data";private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME+ " (" + COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_NAME+ " TEXT," + COLUMN_DATA + " INTEGER NOT NULL)";public MyDataBaseAdapter(Context context){mContext = context;}/** * 打开数据库 */public void open(){dh = new DatabaseHelper(mContext,DB_NAME,null,DB_VERSION,CREATE_TABLE,TABLE_NAME);mSQLiteDatabase = dh.getWritableDatabase();}/** * 关闭数据库 */public void close(){dh.close();}/** * 插入 */public void insert(){try{ContentValues cv = new ContentValues();cv.put(COLUMN_NAME, "张三");cv.put(COLUMN_DATA, 10);mSQLiteDatabase.insert(TABLE_NAME, null, cv);}catch(Exception ex){ex.printStackTrace();}}/** * 修改 */public void update(){try{String sql = "UPDATE "+TABLE_NAME+" SET "+COLUMN_NAME+" ='李四' WHERE  _id=1";mSQLiteDatabase.execSQL(sql);}catch(Exception ex){ex.printStackTrace();}}/** * 修改 */public void delete(){try{mSQLiteDatabase.delete(TABLE_NAME, "_id=2", null);}catch(Exception ex){ex.printStackTrace();}}/** * 查询 */public Cursor select(){Cursor cursor = null;try{String sql = "SELECT * FROM "+TABLE_NAME;cursor = mSQLiteDatabase.rawQuery(sql, null);return cursor;}catch(Exception ex){ex.printStackTrace();return null;}}}


package com.Aina.Android;import android.app.Activity;import android.database.Cursor;import android.graphics.Color;import android.os.Bundle;import android.view.KeyEvent;import android.widget.LinearLayout;import android.widget.ListAdapter;import android.widget.SimpleCursorAdapter;import android.widget.LinearLayout.LayoutParams;import android.widget.ListView;public class Test extends Activity {/** Called when the activity is first created. */private MyDataBaseAdapter mdba = null;private LinearLayout ll = null;private ListView lv = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ll = new LinearLayout(this);ll.setOrientation(LinearLayout.VERTICAL);ll.setBackgroundColor(Color.BLACK);ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));lv = new ListView(this);lv.setBackgroundColor(Color.BLACK);ll.addView(lv, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));setContentView(ll);mdba = new MyDataBaseAdapter(this);mdba.open();this.show();}@Overrideprotected void onPause() {super.onPause();mdba.close();}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_DPAD_LEFT:mdba.insert();break;case KeyEvent.KEYCODE_DPAD_RIGHT:mdba.delete();break;case KeyEvent.KEYCODE_DPAD_UP:mdba.update();break;}this.show();return super.onKeyDown(keyCode, event);}private void show() {Cursor cursor = mdba.select();if (cursor != null) {ListAdapter adapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, cursor, new String[] {MyDataBaseAdapter.COLUMN_NAME,MyDataBaseAdapter.COLUMN_DATA }, new int[] {android.R.id.text1, android.R.id.text2 });lv.setAdapter(adapter);}}}

更多相关文章

  1. 通过CMD 查看android APP数据库表
  2. Android 简单数据库(增删改查)
  3. ch07 Android 日期与时间对话框
  4. Android TimeLine 时间节点轴的实现
  5. Android时间工具类 本地转UTC,UTC转本地
  6. Android sd卡读取数据库实例代码
  7. Android菜鸟日记10 SQLite 数据库

随机推荐

  1. android手机锁屏后service工作
  2. Android中的HandlerThread的理解
  3. Linux下Binder驱动源码(linux-3.0.86)
  4. Android开发书籍书籍介绍:
  5. Android 动态注册监听网络变化实例详解
  6. android上代码去执行"su"命令
  7. android 强制设置横屏 判断是横屏还是竖
  8. android 颜色
  9. android代码混淆及打包相关
  10. Android studio 打包apk报错Execution fa