- package com.Aina.Android;
-
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
-
-
-
-
-
-
-
- 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;
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
-
- db.execSQL(str);
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE "+table);
- this.onCreate(db);
-
- }
- }
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;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(str);
}
@Override
public 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;
-
-
-
-
-
-
-
- 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.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 {
-
- private MyDataBaseAdapter mdba = null;
- private LinearLayout ll = null;
- private ListView lv = null;
-
- @Override
- public 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();
- }
-
- @Override
- protected void onPause() {
- super.onPause();
- mdba.close();
- }
-
- @Override
- public 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);
- }
- }
- }
阅读(1153) | 评论(0) | 转发(0) |