sqlite的使用及其框架

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");

“`

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小阳世界2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值