当前位置:网站首页>创建数据库并使用
创建数据库并使用
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();//查询
边栏推荐
- Excuse me, is it cost-effective to insure sunshine Optimus Prime term life insurance No. 7? What are the advantages of this product?
- Redis CacheClient
- Talk about redis transactions
- Admixture usage document Cookbook
- 2022-06-27日报:Swin Transformer、ViT作者等共话:好的基础模型是CV研究者的朴素追求
- Privacy computing fat offline prediction
- Pychart installation and setup
- Typescript learning materials
- AQS抽象队列同步器
- [WUSTCTF2020]girlfriend
猜你喜欢

洛谷入门2【分支结构】题单题解

基于SSM的Web网页聊天室系统

Is flutter easy to learn? How to learn? The most complete introduction and actual combat of flutter in history. Take it away without thanks~

Buuctf Misc

CAS之比较并交换

The global chip market may stagnate, and China's chip expansion accelerates to improve its self-sufficiency rate against the trend

Great God developed the new H5 version of arXiv, saying goodbye to formula typography errors in one step, and the mobile phone can easily read literature

Hyperledger Fabric 2. X custom smart contract

Knowledge map model

ThreadLocal之强、弱、軟、虛引用
随机推荐
Longest substring without repeated characters (Sword finger offer 48)
Nvidia Deepstream 运行延迟,卡顿,死机处理办法
Redis CacheClient
[digital signal processing] discrete time signal (analog signal, discrete time signal, digital signal | sampling leads to time discrete | quantization leads to amplitude discrete)
I want to buy fixed income + products, but I don't know what its main investment is. Does anyone know?
基于WEB平台的阅读APP设计与实现
可变参数模板 Variadic Templates
Typescript learning materials
Using redis skillfully to realize the like function, isn't it more fragrant than MySQL?
Derivation of Halcon camera calibration principle
Make a ThreadLocal (source code) that everyone can understand
注解学习总结
R language triple becomes matrix matrix becomes triple
手机号码的格式
[advanced mathematics] from normal vector to surface integral of the second kind
Google tool splits by specified length
原子操作类
How is the London Silver point difference calculated
AQS Abstract queue synchronizer
#27ES6的数值扩展