赞
踩
package com.example.bookdemo2; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DBOpenHelper2 extends SQLiteOpenHelper { public DBOpenHelper2(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { String sql="create table book(id integer primary key autoincrement,bookname varchar(20),bookauthor varchar(20),booktextnumber integer)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
其中oncreate中的sql语句用于创建表,以及表的字段
DBOpenHelper2 dbsqLiteOpenHelper = new DBOpenHelper2(MainActivity.this, "books.db", null, 1); db = dbsqLiteOpenHelper.getWritableDatabase(); //创建存放数据的ContentValues对象 ContentValues values = new ContentValues(); values.put("bookname", "刁民王小二"); values.put("bookauthor", "拾伍"); values.put("booktextnumber", "1991000" + "个字"); Log.e("moli", "添加书籍1成功"); //数据库执行插入命令 db.insert("book", null, values); values.put("bookname", "万夜之主"); values.put("bookauthor", "一柄墨刀"); values.put("booktextnumber", "3764000" + "个字"); //数据库执行插入命令 db.insert("book", null, values); Log.e("moli", "添加书籍2成功");
db.delete("book", null, null);
bookList.clear();
一个是删数据库,一个是删list列表
ContentValues values = new ContentValues();
values.put("price", 10.99);
db.update("Book", values, "name = ?", new String[] { "The Da Vinci Code" }); }
//创建游标对象
Cursor cursor = db.query("book", new String[]{"id","bookname","bookauthor","booktextnumber"}, null, null, null, null, null);
//利用游标遍历所有数据对象
while(cursor.moveToNext()){
@SuppressLint("Range") String id = cursor.getString(cursor.getColumnIndex("id"));
@SuppressLint("Range") String bookname = cursor.getString(cursor.getColumnIndex("bookname"));
@SuppressLint("Range") String bookauthor=cursor.getString(cursor.getColumnIndex("bookauthor"));
@SuppressLint("Range") String booktextnumber = cursor.getString(cursor.getColumnIndex("booktextnumber"));
Log.i("moli","result: id=" + id +" bookname: " + bookname +" bookauthor: "+bookauthor+" booktextnumber: "+ booktextnumber);
Book book=new Book(bookauthor,bookname,booktextnumber,"1");
bookList.add(book);
}
// 关闭游标,释放资源
cursor.close();
最后记得设置适配器来更新UI界面
recyclerViewAdapter = new RecyclerViewAdapter(bookList);
recyclerViewAdapter.notifyItemInserted(0);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerViewAdapter.notifyDataSetChanged();
SQLiteOpenHelper是个抽象类,没有实现类,所以要创建实现类来继承SQLiteOpenHelper,来重写方法
按ctrl+鼠标左键点击SQLiteOpenHelper,可查看源代码。看官方注释就能看到各参数的作用
游标cursor 作用类似于可以游动的指针,用于定位数据库的元素
Context为内容,哪个activity要使用数据库就用哪个context
这两行代码用于创建数据库文件
DatabaseHelper helper=new DatabaseHelper(this);//创建DatabaseHelper的对象
helper.getWritableDatabase();//创建数据库
ctrl+alt+f 使变量成为全局变量
代码如下
DBOpenHelper2.java
package com.example.bookdemo2; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class DBOpenHelper2 extends SQLiteOpenHelper { public DBOpenHelper2(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { String sql="create table book(id integer primary key autoincrement,bookname varchar(20),bookauthor varchar(20),booktextnumber integer)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
SQLiteDatabase里面的简单操作数据库的方法
第一行代码第二版
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。