DBHelper
@H_419_6@
package com.example.ex_2;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androID.util.Log;public class DbHelper extends sqliteOpenHelper { private static final String DATABASE_name = "test.db"; //数据库名字 private static final int DATABASE_VERSION = 1 ; //数据库版本号 private static final String table_ = "remark"; private static final String TAG = "mydeBUG"; //创建 students 表的 sql 语句 private static final String CReate_sql = "create table if not exists "+ table_ + "(" + "ID integer primary key autoincrement," + "Title varchar(100) not null," + "content varchar(1000)," + "creat_time datetime not null," + "noti_time datetime not null" + ");"; public DbHelper(Context context){ super(context, "test.db", null, 1); } /*创建数据库 * ID是主键自增*/ @OverrIDe public voID onCreate(sqliteDatabase db) { Log.d(TAG, "onCreate: "); db.execsql(CReate_sql); } /** * 当检测与前一次创建数据库版本不一样时,先删除表再创建新表 */ @OverrIDe public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { db.execsql("DROP table IF EXISTS " + table_); onCreate(db); } /** * 变更列名 * @param db * @param oldColumn * @param newColumn * @param typeColumn */ public voID updateColumn(sqliteDatabase db, String oldColumn, String newColumn, String typeColumn){ try{ db.execsql("ALTER table " + table_ + " CHANGE " + oldColumn + " "+ newColumn + " " + typeColumn ); }catch(Exception e){ e.printstacktrace(); } }}VIEw Code
增删改查
package com.example.ex_2;import androID.content.ContentValues;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.Cursor;import androID.util.Log;import java.util.ArrayList;import java.util.List;public class sql { private static String TAG = "mydegBUG"; private DbHelper dbHelper; private sqliteDatabase db; public sql(Context context) { Log.d(TAG, "sql: init"); System.out.println("创建数据库"); /* 初始化并创建数据库 */ dbHelper = new DbHelper(context);// dbHelper.getWritableDatabase(); } //增 /* * remark 要存入数据库的实体类 */ public voID add(BeanRemark remark) { dbHelper.updateColumn(db, "crea_time", "creat_time", "datetime"); Log.d(TAG, "add: Zeng"); db = dbHelper.getWritableDatabase(); //调用sqliteHelper.OnCreat() //用容器存储 ContentValues values = new ContentValues(); values.put("Title", remark.getTitle()); values.put("content", remark.getContent()); values.put("noti_time", remark.getNoti_time()); values.put("creat_time", remark.getCreat_time()); long result = db.insert("remark", null, values); db.close(); Log.d(TAG, "add: " + result); } //删 //传入要删除对象的ID 空的就全删 public voID delete(List<String> remarksID) { sqliteDatabase db = dbHelper.getWritableDatabase(); Log.d(TAG, "delete: " + remarksID); int result = 0; if(!remarksID.isEmpty()) { for(String ID : remarksID) result = db.delete("remark", "ID = ?", new String[]{ID}); } else result = db.delete("remark", null,null); db.close(); Log.d(TAG, "delete: res = " + result); } public voID update(BeanRemark remark) { sqliteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); if(remark.getTitle() != null) values.put("Title", remark.getTitle()); if(remark.getContent() != null) values.put("content", remark.getContent()); if(remark.getNoti_time() != null) values.put("noti_time", remark.getNoti_time()); if(remark.getCreat_time() != null)values.put("creat_time", remark.getCreat_time()); int result = db.update("remark", values, "ID=?", new String[] {""+remark.getID()}); db.close(); Log.d(TAG, "update: res = " + result); } public List<BeanRemark> find(BeanRemark remark) { sqliteDatabase db = dbHelper.getWritableDatabase(); List<String> selections = new ArrayList(); List<String> vals = new ArrayList(); //判断 remark 里的哪些量不为空 以此来作为where 条件 if(remark.getID() != -1) { selections.add("ID = ?"); vals.add((String) (""+remark.getID())); } if(remark.getTitle() != null) { selections.add("Title = ?"); vals.add((String) remark.getTitle()); } if(remark.getContent() != null) { selections.add("content = ?"); vals.add((String) remark.getContent()); } if(remark.getNoti_time() != null) { selections.add("noti_time = ?"); vals.add((String) remark.getNoti_time()); } if(remark.getCreat_time() != null) { selections.add("Creat_time = ?"); vals.add((String) remark.getCreat_time()); } String selection = new String(); for(int i = 0; i < selections.size(); i++) { if(i != 0) selection += "and"; selection += selections.get(i); } if(selections.size() > 1) { selection += "and"; selection += selections.get(selections.size()-1); } Log.d(TAG, "find: " + selection); Log.d(TAG, "find2: " + vals.toArray(new String[vals.size()])); Cursor cursor = db.query("remark", null, selection, vals.toArray(new String[vals.size()]), null, null, null); List<BeanRemark> remarks = new ArrayList<BeanRemark>(); while (cursor.movetoNext()) { int ID = cursor.getInt(cursor.getColumnIndex("ID")); String Title = cursor.getString(cursor.getColumnIndex("Title")); String content = cursor.getString(cursor.getColumnIndex("content")); String noti_time = cursor.getString(cursor.getColumnIndex("noti_time")); String creat_time = cursor.getString(cursor.getColumnIndex("creat_time")); remarks.add(new BeanRemark(ID, Title, creat_time, noti_time, content)); } db.close(); return remarks; } public List<BeanRemark> findAll() { sqliteDatabase db = dbHelper.getWritableDatabase(); Cursor cursor = db.query("remark", null, null, null, null, null,null); List<BeanRemark> remarks = new ArrayList<BeanRemark>(); BeanRemark remark = null; while (cursor.movetoNext()) { int ID = cursor.getInt(cursor.getColumnIndex("ID")); String Title = cursor.getString(cursor.getColumnIndex("Title")); String content = cursor.getString(cursor.getColumnIndex("content")); String noti_time = cursor.getString(cursor.getColumnIndex("noti_time")); String creat_time = cursor.getString(cursor.getColumnIndex("creat_time")); remarks.add(new BeanRemark(ID, Title, creat_time, noti_time, content)); } db.close(); return remarks; }}VIEw Code
使用
package com.example.ex_2;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androIDx.appcompat.app.AppCompatActivity;import java.util.ArrayList;import java.util.List;public class Test extends AppCompatActivity { private static String TAG = "mydeBUG"; private sql sql; private button btn1,btn2,btn3,btn4; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_test); sql = new sql(this); Log.d(TAG, "onCreate: " + new BeanRemark()); Log.d(TAG, "onCreate: " + sql); btn1 = findVIEwByID(R.ID.Zeng); btn1.setonClickListener(new mClick()); btn2 = findVIEwByID(R.ID.shan); btn2.setonClickListener(new mClick()); btn3 = findVIEwByID(R.ID.gai); btn3.setonClickListener(new mClick()); btn4 = findVIEwByID(R.ID.cha); btn4.setonClickListener(new mClick()); } class mClick implements OnClickListener { @OverrIDe public voID onClick(VIEw arg) { switch (arg.getID()) { case R.ID.Zeng: sql.add(new BeanRemark("wang", Time.CurTime(), "2019-12-20 16:32:00","sds")); sql.add(new BeanRemark("wang", Time.CurTime(), "2019-12-20 16:32:00","sss")); sql.add(new BeanRemark("li", Time.CurTime(), "2019-12-20 16:32:00","sds")); Log.d(TAG, "onClick: Zeng"); break; case R.ID.shan: Log.d(TAG, "onClick: shan"); List<BeanRemark> List4 = sql.find(new BeanRemark(null, null, null, "sds")); List<String> lisID = new ArrayList(); for(BeanRemark item : List4) lisID.add(""+item.getID()); sql.delete(lisID); break; case R.ID.gai: Log.d(TAG, "onClick: gai"); sql.update(new BeanRemark(2,null,null,null,"你是傻逼")); break; case R.ID.cha: Log.d(TAG, "onClick: cha"); List<BeanRemark> List = sql.findAll(); Log.d(TAG, "onClick: " + List + "\n"); List<BeanRemark> List2 = sql.find(new BeanRemark(null, null, null, "sds")); Log.d(TAG, "onClick: " + List2); List<BeanRemark> List3 = sql.find(new BeanRemark(2,null,null,null,null)); Log.d(TAG, "onClick: find 2 : " + List3); break; } } }}VIEw Code
实体类
package com.example.ex_2;public class BeanRemark { private int ID = -1; private String Title; private String creat_time; private String noti_time; private String content; public BeanRemark() { } public BeanRemark(int ID) { this.ID = ID; } public BeanRemark(String Title, String creat_time, String noti_time, String content) { this.Title = Title; this.creat_time = creat_time; this.noti_time = noti_time; this.content = content; } public BeanRemark(int ID, String Title, String creat_time, String noti_time, String content) { this.ID = ID; this.Title = Title; this.creat_time = creat_time; this.noti_time = noti_time; this.content = content; } public int getID() { return ID; } public voID setID(int ID) { this.ID = ID; } public String getTitle() { return Title; } public voID setTitle(String Title) { this.Title = Title; } public String getCreat_time() { return creat_time; } public voID setCreat_time(String creat_time) { this.creat_time = creat_time; } public String getNoti_time() { return noti_time; } public voID setNoti_time(String noti_time) { this.noti_time = noti_time; } public String getContent() { return content; } public voID setContent(String content) { this.content = content; } @OverrIDe public String toString() { return "BeanRemark{" + "ID=" + ID + ", Title='" + Title + '\'' + ", creat_time='" + creat_time + '\'' + ", noti_time='" + noti_time + '\'' + ", content='" + content + '\'' + '}'; }}VIEw Code 总结
以上是内存溢出为你收集整理的Android Sqlite全部内容,希望文章能够帮你解决Android Sqlite所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)