当前位置:网站首页>创建数据库并使用
创建数据库并使用
2022-06-27 15:23:00 【android练习生】
1.创建数据库类继承SQLiteOpenHelper,在重写的onCreate()方法中调用 db.execSQL(CREATE_BOOK)sql语句创建数据库,名称为Book。
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK = "create table Book ("
+ "phone integer primary key , "
+ " username text , "
+ "images BLOB ,"
+ "password text )";
public static final String CREATE_ADDRESS = "create table Address ("
+ "number text , "
+ "name text )";
private Context mContext;
public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable 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_ADDRESS);
Toast.makeText(mContext, "Create succeeded", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
2.在活动中使用SQLiteDatabase进行增删改查
MyDatabaseHelper dbHelper_address = new MyDatabaseHelper(getActivity(), "BookStore.db", null, 1);
SQLiteDatabase db_address = dbHelper_address.getWritableDatabase();
db_address.execSQL("delete from Address where name = ?", new String[]{name1});;//删除
db_address.execSQL("insert into Address (number, name) values( ? , ?)",
new String[]{number, name});//增加
Cursor cursorsearch = db_address.rawQuery("select * from Address where name like '%"+search+"%'" , null);
while (cursorsearch.moveToNext()) {
@SuppressLint("Range") String sname = cursorsearch.getString(cursorsearch.getColumnIndex("name"));
@SuppressLint("Range") String snumber = cursorsearch.getString(cursorsearch.getColumnIndex("number"));
Address_item_bean searchInfo = new Address_item_bean(sname, snumber);
list2.add(searchInfo); }
cursorsearch.close();//查询
边栏推荐
- Interpretation of new version features of PostgreSQL 15 (including live Q & A and PPT data summary)
- Vscode uses yapf auto format to set the maximum number of characters per line
- 【kotlin】第二天
- Abnormal analysis of pcf8591 voltage measurement data
- Piblup test report 1- pedigree based animal model
- Pychart installation and setup
- 16 -- 删除无效的括号
- CAS comparison and exchange
- PCL Library - error reporting solution: cmake and Anaconda conflicts during installation
- [issue 17] golang's one-year experience in developing Meitu
猜你喜欢
关于TensorFlow使用GPU加速
Make a ThreadLocal (source code) that everyone can understand
ThreadLocal之强、弱、軟、虛引用
At a time of oversupply of chips, China, the largest importer, continued to reduce imports, and the United States panicked
All you want to know about large screen visualization is here
Leetcode 724. 寻找数组的中心下标(可以,一次过)
海量数据!秒级分析!Flink+Doris构建实时数仓方案
Introduction to TTCAN brick moving
Référence forte, faible, douce et virtuelle de threadlocal
Knowledge map model
随机推荐
Leetcode 724. 寻找数组的中心下标(可以,一次过)
Synchronized与锁升级
Using redis skillfully to realize the like function, isn't it more fragrant than MySQL?
February 16, 2022 freetsdb compilation and operation
直播app运营模式有哪几种,我们该选择什么样的模式?
QT 如何在背景图中将部分区域设置为透明
Atomic operation class
Can the teacher tell me what the fixed income + products are mainly invested in?
PostgreSQL 15新版本特性解读(含直播问答、PPT资料汇总)
I want to buy fixed income + products, but I don't know what its main investment is. Does anyone know?
Hyperledger Fabric 2. X custom smart contract
Elegant custom ThreadPoolExecutor thread pool
522. 最长特殊序列 II / 剑指 Offer II 101. 分割等和子集
优雅的自定义 ThreadPoolExecutor 线程池
Strong, weak, soft and virtual references of ThreadLocal
Pri3d: a representation learning method for 3D scene perception using inherent attributes of rgb-d data
AQS Abstract queue synchronizer
Je veux acheter des produits à revenu fixe + mais je ne sais pas quels sont ses principaux investissements.
Référence forte, faible, douce et virtuelle de threadlocal
What are the characteristics of fixed income + products?