sqlite的使用及其框架
16lz
2021-01-26
sqlite
public static final String CREATE_BOOK = "create table Book(" + "id Integer primary key autoincrement," + "author text," + "pages integer," + "name text)"; public static final String CREATE_CATEGORY = "create table Category(" + "id integer primary key autoincrement," + "category_name text," + "category_code integer)"; private Context mContext; public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); mContext = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_BOOK); db.execSQL(CREATE_CATEGORY); Toast.makeText(mContext, "Create Success", Toast.LENGTH_LONG).show(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exits Book"); db.execSQL("drop table if exits Category"); onCreate(db); }
dbHelper = new MyDatabaseHelper(this, "Book", null, 2);//初始化
SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues();//通过一个value values.put("name", "Ming"); values.put("author", "MIng"); values.put("pages", 13); values.put("prices", 15.1); db.insert("Book", null, values);//插入 values.clear();//清空 db.insert("Book", null, values); db.update("Book", values, "name=?", new String[]{"djkfh"});//更新数据 db.delete("Book", "name=?", new String[]{"kld"});//删除数据
Cursor cursor = db.query("Book", null, null, null, null, null, null); if (cursor.moveToFirst()) { do { String name = cursor.getString(cursor.getColumnIndex("name")); int pages = cursor.getInt(cursor.getColumnIndex("pages"));}while(cursor.moveToNext());
当然也提供了数据库的常规操作方法
db.execSQL("insert into Book (name,author,pages,price)values(?,?,?,?)", new String[]{"The", "df", "4", "3"});db.execSQL("update Book set price = ? where name = ?", new String[]{"f", "jdfh"});db.execSQL("delete from Book where pages>", new String[]{"dh"});db.rawQuery("select *from Book", null);
sqlite框架LitePal
开源的数据库androd框架,采用了对象关系映射,将数据库功能进行了封装,可以轻松完成增删改查的操作,使用文档地址是 https://github.com/LitePalFramework/LitePal
首先先要添加依赖包,不提了。
然后在创建assets目录,在main中创建Directory建立litepal.xml文件。接着编辑
<?xml version="1.0" encoding="utf-8"?><litepal><dbname value = "BookStore">dbname><version value = "1">version><list>list>litepal>
其他的需要在application中初始化,LitePal.initialize(this);
public class Album extends DataSupport { @Column(unique = true, defaultValue = "unknown") private String name; private float price; private byte[] cover; private List songs = new ArrayList(); // generated getters and setters. ...}public class Song extends DataSupport { @Column(nullable = false) private String name; private int duration; @Column(ignore = true) private String uselessField; private Album album; // generated getters and setters. ...}Then add these models into the mapping list in litepal.xml: "org.litepal.litepalsample.model.Album" /> "org.litepal.litepalsample.model.Song" />
其他操作请参看官方文档,还有一个DataSupport类需要了解。
LitePal还支持原生的sql语句进行操作们还支持链式编程
//查询Book表中11-20条满足页数大于400这个条件的name,author,price这3列数据,并按照页数升序排列List books = DataSupport.select("name","author","price") .where("pages>?","400") .order("pages") .limit(10) .offset(10) .find(Book.class); //原生查询 Cursor cursor = DataSupport.findBySQL("select from Book where pages > ?and prices < ?","400","20");
“`
更多相关文章
- 使用ThinkAndroid快速开发框架需要有以下权限
- WMS总体框架
- Android学习笔记(21)————利用JDBC连接服务器数据库
- Android(安卓)优秀开源框架集合
- android项目中单实例数据库类
- Android主流框架学习之旅
- Android下建立数据库
- androidj常用数据库操作JDBC Utils
- 模拟按键操作的几种方式