当前位置:网站首页>创建数据库并使用
创建数据库并使用
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();//查询
边栏推荐
- 关于 Spartacus 的 sitemap.xml 问题
- Synchronized and lock escalation
- ThreadLocal之强、弱、軟、虛引用
- Creation and use of static library (win10+vs2022
- Julia1.1 installation instructions
- NLP - monocleaner
- [MySQL] query valid data based on time
- [advanced mathematics] from normal vector to surface integral of the second kind
- Leetcode 724. 寻找数组的中心下标(可以,一次过)
- February 16, 2022 freetsdb compilation and operation
猜你喜欢

R language objects are stored in JSON

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

Reflection learning summary

反射学习总结

Talk about redis transactions

volatile与JMM

Knowledge map model

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

PostgreSQL 15新版本特性解读(含直播问答、PPT资料汇总)

Design and implementation of reading app based on Web Platform
随机推荐
Sword finger offer II 039 Histogram maximum rectangular area monotonic stack
Hyperledger Fabric 2. X custom smart contract
How QT sets some areas to be transparent in the background image
Strong, weak, soft and virtual references of ThreadLocal
Volatile and JMM
What is the London Silver unit
[an Xun cup 2019]attack
Je veux acheter des produits à revenu fixe + mais je ne sais pas quels sont ses principaux investissements.
[issue 18] share a Netease go classic
Professor huangxutao, a great master in CV field, was born at the age of 86. UIUC specially set up a doctoral scholarship to encourage cutting-edge students
Knightctf 2022 web section
洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举
阅读别人的代码,是一种怎样的体验
Integration of entry-level SSM framework based on XML configuration file
关于TensorFlow使用GPU加速
What are the characteristics of fixed income + products?
Why can't the start method be called repeatedly? But the run method can?
基于SSM的Web网页聊天室系统
February 16, 2022 freetsdb compilation and operation
Google tool splits by specified length