1、数据库的建立
package my.pack;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import java.util.*;
import android.content.Context;
import android.database.*;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class MyDatabaseHelper extends SQLiteOpenHelper
{
private static final String DB_NAME = "Notepad_1.db";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "Table_1";
MyDatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String Sql = "CREATE TABLE Table_1 ('Time' varchar(200) not null,'Title' varchar (200) not null,'Content' text ,primary key(Title));";
db.execSQL(Sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
}
}
2、数据库操作帮组类(选择、插入、删除、)
package my.pack;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class MySqlHelper {
public MyDatabaseHelper mOpenHelper;
public SQLiteDatabase db;
//构造函数
MySqlHelper(Context context)
{
mOpenHelper = new MyDatabaseHelper(context);
}
//选择
public int SelectItem(String mytitle)
{
db = mOpenHelper.getReadableDatabase();
//String col[] = {"Time","Title","Content"};
//String Sql = "where 'Title' = " + mytitle;
//Cursor cur = db.query("Table_1",col,Sql,null,null,null,null);
Cursor cur = db.rawQuery("SELECT * FROM Table_1 WHERE Title=?", new String[] { mytitle});
int num = cur.getCount();
cur.close();
db.close();
return num;
}
//插入
public void InsertItem(String mytime,String mytitle,String mycontent)
{
db = mOpenHelper.getWritableDatabase();
String Sql= "insert into Table_1 values('" + mytime +"', '" + mytitle +"', '" + mycontent+"' )";
try
{
db.execSQL(Sql);
//setTitle("Yes");
}
catch(SQLException e)
{
//setTitle(Sql);
}
db.close();
}
//显示
public ArrayList<HashMap<String, Object>> ShowItem()
{
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
db = mOpenHelper.getReadableDatabase();
String col[] = {"Time","Title","Content"};
Cursor cur = db.query("Table_1",col,null,null,null,null,"Time desc"); //按时间降序
int num = cur.getCount();
//setTitle(Integer.toString(num) + " 条记录");
cur.moveToFirst();
for(int i=0;i<num;i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemTitle", cur.getString(1));
map.put("ItemText", cur.getString(0));
listItem.add(map);
cur.moveToNext();
}
cur.close();
db.close();
return listItem;
}
//删除Item
public void DeleteItem(String mytitle)
{
try
{
db = mOpenHelper.getWritableDatabase();
db.delete("Table_1","Title = '"+mytitle + "'",null);
//setTitle("删除Title为" + mytitle +"的记录");
db.close();
}
catch(SQLException e)
{}
}
//删除表项
public void DeleteTable(String mytable)
{
try
{
db = mOpenHelper.getWritableDatabase();
String Sql = "drop table " +mytable;
db.execSQL(Sql);
db.close();
//setTitle("已删除" + mytable +"表项");
}
catch(SQLException e)
{}
}
}
3、删除数据库
**删除数据库
final File file = this.getDatabasePath(DB_NAME);
file.delete();
Ps、 第一次调用getWritableDatabase()或getReadableDatabase()方法后,SQLiteOpenHelper会缓存当前的SQLiteDatabase实例,SQLiteDatabase实例正常情况下会维持数据库的打开状态,所以在你不再需要SQLiteDatabase实例时,请及时调用close()方法释放资源。一旦SQLiteDatabase实例被缓存,多次调用getWritableDatabase()或getReadableDatabase()方法得到的都是同一实例。