1、SQLiteOpenHelper的介绍:
A helper class to manage database creation and version management.
也就是说SQLiteOpenHelper用来管理创建一个数据库和管理数据库的版本。
在继承SQLiteOpenHelper的类(extends
SQLiteOpenHelper
)里面,通过复写
onCreate(SQLiteDatabase)
,onUpgrade(SQLiteDatabase, int, int)
和onOpen(SQLiteDatabase)
(可选)来操作数据库。
创建一个Datebase Helper类:
public class DatabaseHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; String DATABASE_CREATE = "create table user(_id INTEGER PRIMARY KEY AUTOINCREMENT,mtext varchar(8000),mtime varchar(200))"; // 构造函数 public DatabaseHelper(Context context, String DATABASE_NAME) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("create my idea.sql"); // 可创建两张表 db.execSQL(DATABASE_CREATE);// 重要,这个只会在第一次创建数据库时才会调用的,或者删除了已创的数据库才会再次调用 // db.execSQL(DATABASE_CREATE1); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub System.out.println("update a Database"); }
2、在Activity中进行对数据库的操作:
//添加数据 public void addData() { ContentValues values = new ContentValues(); // values.put("id", 1);//这里使用了固定的构造表时的默认的_id,所以这句就可省略了 values.put("mtext", "name"); values.put("mtime", "idea.com"); SQLiteDatabase db = dbHelper.getWritableDatabase(); long rowid = db.insert("user", null, values);// 在表里插入数据,并返回插入所在的_id System.out.println("-->" + rowid); // mlayout.postInvalidate(); db.close(); } //查询数据 public void query() { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query("user", null, null, null, null, null, null); while (cursor.moveToNext()) { String mtext = cursor.getString(cursor.getColumnIndex("mtext")); String mtime = cursor.getString(cursor.getColumnIndex("mtime")); System.out .println("" + cursor.getInt(cursor.getColumnIndex("_id"))); System.out.println("query--->" + mtext + " " + mtime); } db.close(); } //更新数据 public void updataData() { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("mtext", "name1"); values.put("mtime", "258963"); db.update("user", values, "_id=?", new String[] { "4" });// 更新_id为4的数据 // db.update("users", values, null, null);//更新表中所有数据 db.close(); } //删除指定记录 public void deleteData() { SQLiteDatabase db = dbHelper.getReadableDatabase(); // 删除表的所有数据 // db.delete("users",null,null); // 从表中删除指定的一条数据 db.execSQL("DELETE FROM " + "user" + " WHERE _id=" + Integer.toString(2)); db.close(); } //删除表或者整个数据库 public void deleteTable() { SQLiteDatabase db = dbHelper.getReadableDatabase(); try { // 执行SQL语句 db.execSQL("drop table user"); // 删除表 } catch (SQLException e) { } this.deleteDatabase("idea.sql");// 删除一个数据库,只要有这个数据库,填入数据库名即可删除 db.close(); }
3、ListView的操作
main.xml
<ListView android:paddingTop="30dp" android:id="@+id/mlistview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1" android:shrinkColumns="1,2" > <TableRow> <TextView android:id="@+id/ItemText" android:text="TItemTitle" android:layout_width="320dp" android:layout_height="36dp" android:textSize="25dp" android:layout_gravity="left" android:gravity="left" android:paddingLeft="10dp" /> <TextView android:id="@+id/ItemTime" android:text="ItemTime" android:textSize="25dp" android:layout_width="260dp" android:layout_height="36dp" android:layout_gravity="right" android:gravity="left" android:paddingRight="10dp"/> </TableRow> </TableLayout>
变量声明:
private ListView mlistview; private SimpleAdapter mlistItemAdapter; private ArrayList<HashMap<String, Object>> mlistItem = null; private DatabaseHelper dbHelper;
mlistview = (ListView) findViewById(R.id.mlistview); mlistItem = new ArrayList<HashMap<String, Object>>(); dbHelper = new DatabaseHelper(NoteActivity.this, "idea.sql"); SQLiteDatabase db = dbHelper.getReadableDatabase(); // 以下是把所有的表都存进来,然后_id按顺序排列,方便读取数据 Cursor cursor = db.query("user", null, null, null, null, null, null); while (cursor.moveToNext()) { String mtext = cursor.getString(cursor.getColumnIndex("mtext")); String mtime = cursor.getString(cursor.getColumnIndex("mtime")); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("mtext", mtext); map.put("mtime", mtime); mlistItem.add(map); } mlistItemAdapter = new SimpleAdapter( getApplicationContext(), mlistItem,// 数据源 R.layout.listviewitem, new String[] { "mtext", "mtime" }, new int[] { R.id.ItemText, R.id.ItemTime }); mlistview.setAdapter(mlistItemAdapter); db.close(); // 点击 mlistview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } }); // 添加长按点击 mlistview .setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("选择操作"); menu.add(0, 0, 0, "删除"); menu.add(0, 1, 1, "返回"); } }); } // 长按菜单响应函数 @Override public boolean onContextItemSelected(MenuItem item) { return super.onContextItemSelected(item); }