SQLite數據庫


SQLiteOpenHeleper
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "new.db";
private static final int VERSION = 1;

//建立庫
public MySQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}

//建立表 第一次建立時調用,以後再不調用
@Override
public void onCreate(SQLiteDatabase db) {
String sql = Constant.NewsTable.getCreateTableSQL();
db.execSQL(sql);
}
//更新表
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table " + Constant.NewsTable.TBL_NAME);
onCreate(db);
}
}
工具類,SQL語句
public class Constant {
public static class NewsTable{
public static final String TBL_NAME = "NEWS";
public static final String TBL_COL_TITLE = "NEWSTITLE";
public static final String TBL_COL_IMG = "NEWSIMG";
public static final String TBL_COL_SRC = "NEWSSRC";
public static final String TBL_COL_COMM = "NEWSCOMM";
public static final String TBL_COL_DATE = "NEWSDATE";

public static String getCreateTableSQL(){
String sql = "CREATE TABLE IF NOT exists "
+ TBL_NAME
+ "("
+ " _id integer primary key autoincrement ,"
+ TBL_COL_TITLE + " text,"
+ TBL_COL_IMG + " integer,"
+ TBL_COL_SRC + " text,"
+ TBL_COL_COMM + " text,"
+ TBL_COL_DATE + " varchar(50)"
+ ")";
return sql;
}
}
}
增刪改查public void insertdata(){    //調用幫助類的構造函數,去建立數據庫    MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);    //調用幫助類的onCreate方法,去建立表    SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();    //準備要保存的數據    ContentValues contentValues = new ContentValues();    contentValues.put(Constant.NewsTable.TBL_COL_TITLE,text1.getText().toString());    contentValues.put(Constant.NewsTable.TBL_COL_IMG,text2.getText().toString());    contentValues.put(Constant.NewsTable.TBL_COL_SRC,text3.getText().toString());    contentValues.put(Constant.NewsTable.TBL_COL_COMM,text4.getText().toString());    contentValues.put(Constant.NewsTable.TBL_COL_DATE,text5.getText().toString());    //調用insert方法,去保存數據    sqLiteDatabase.insert(Constant.NewsTable.TBL_NAME,null,contentValues);    //關閉sqLiteDatabase數據庫    sqLiteDatabase.close();    Toast.makeText(getApplication(),"保存數據成功",Toast.LENGTH_SHORT).show();    /*listView = (ListView) findViewById(R.id.SQLite);    list = new ArrayList<Map<String,String>>();    Map<String,String> map1 = new HashMap<String, String>();    map1.put("title",text1.getText().toString());    map1.put("img",text2.getText().toString());    map1.put("src",text3.getText().toString());    map1.put("comm",text4.getText().toString());    map1.put("date",text5.getText().toString());    list.add(map1);*/}    public void querydata(){        MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);        SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getReadableDatabase();        Cursor cursor = sqLiteDatabase.query(                Constant.NewsTable.TBL_NAME,                new String[]{Constant.NewsTable.TBL_COL_TITLE,Constant.NewsTable.TBL_COL_DATE},                null,null,null,null,null);        while (cursor.moveToNext()){            int titleIndex = cursor.getColumnIndex(Constant.NewsTable.TBL_COL_TITLE);            String title = cursor.getString(titleIndex);            int dateIndex = cursor.getColumnIndex(Constant.NewsTable.TBL_COL_DATE);            String date = cursor.getString(dateIndex);            Toast.makeText(getApplication(),title+date,Toast.LENGTH_SHORT).show();            text1.setText(title);            text5.setText(date);        }        cursor.close();        sqLiteDatabase.close();        Toast.makeText(getApplication(),"查詢成功!!!",Toast.LENGTH_SHORT).show();    }    public void deletedata(){        MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);        SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();        sqLiteDatabase.delete(                Constant.NewsTable.TBL_NAME,                Constant.NewsTable.TBL_COL_TITLE+" = ? ",                new String[]{text1.getText().toString()}        );        sqLiteDatabase.close();        Toast.makeText(getApplication(),"刪除成功!!",Toast.LENGTH_SHORT).show();    }    public void updatedata(){        MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);        SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();        String title = text1.getText().toString();        ContentValues contentValues = new ContentValues();        contentValues.put(Constant.NewsTable.TBL_COL_TITLE,title);        sqLiteDatabase.update(                Constant.NewsTable.TBL_NAME,                contentValues,                Constant.NewsTable.TBL_COL_TITLE+" = ? ",                new String[]{text1.getText().toString()}        );        text1.setText(title);        sqLiteDatabase.close();        Toast.makeText(getApplication(),"更改完成!!",Toast.LENGTH_SHORT).show();    }
相關文章
相關標籤/搜索