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<Song> songs = new ArrayList<Song>();
// 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:
<list>
<mapping class="org.litepal.litepalsample.model.Album" />
<mapping class="org.litepal.litepalsample.model.Song" />
</list>
其他操作请参看官方文档,还有一个DataSupport类需要了解。
LitePal还支持原生的sql语句进行操作们还支持链式编程
//查询Book表中11-20条满足页数大于400这个条件的name,author,price这3列数据,并按照页数升序排列
List<Book> 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");
“`