当前位置:网站首页>创建数据库并使用
创建数据库并使用
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();//查询
边栏推荐
- 专用发票和普通发票的区别
- ReentrantLock、ReentrantReadWriteLock、StampedLock
- #27ES6的数值扩展
- 【170】PostgreSQL 10字段类型从字符串修改成整型,报错column cannot be cast automatically to type integer
- 2022-2-15 learning the imitated Niuke project - Section 5 shows comments
- 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
- Introduction to TTCAN brick moving
- 16 -- 删除无效的括号
- Référence forte, faible, douce et virtuelle de threadlocal
- Excuse me, is it cost-effective to insure sunshine Optimus Prime term life insurance No. 7? What are the advantages of this product?
猜你喜欢

Design and implementation of food recipe and ingredients website based on vue+node+mysql

Pri3d: a representation learning method for 3D scene perception using inherent attributes of rgb-d data

Privacy computing fat offline prediction

2022-2-16 learning the imitated Niuke project - Section 6 adding comments

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

Design and implementation of reading app based on Web Platform

Top ten Devops best practices worthy of attention in 2022

Integration of entry-level SSM framework based on XML configuration file

Synchronized and lock escalation

海量数据!秒级分析!Flink+Doris构建实时数仓方案
随机推荐
What are the operating modes of the live app? What mode should we choose?
CV领域一代宗师黄煦涛教授86岁冥诞,UIUC专设博士奖学金激励新锐
Abnormal analysis of pcf8591 voltage measurement data
QT notes (XXVIII) using qwebengineview to display web pages
[microservices sentinel] hotspot rules | authorization rules | cluster flow control | machine list
The global chip market may stagnate, and China's chip expansion accelerates to improve its self-sufficiency rate against the trend
On traversal of tree nodes
Creation and use of static library (win10+vs2022
Interpretation of new version features of PostgreSQL 15 (including live Q & A and PPT data summary)
In the past, domestic mobile phones were arrogant in pricing and threatened that consumers would like to buy or not, but now they have plummeted by 2000 for sale
PR second training notes
QT 如何在背景图中将部分区域设置为透明
[WUSTCTF2020]girlfriend
Piblup test report 1- pedigree based animal model
HTTP Caching Protocol practice
洛谷_P1003 [NOIP2011 提高组] 铺地毯_暴力枚举
Pycharm安装与设置
Go error collection | when a function uses a return value with a parameter name
[an Xun cup 2019]attack
Lei Jun lost another great general, and liweixing, the founding employee of Xiaomi No. 12, left his post. He once had porridge to create Xiaomi; Intel's $5.4 billion acquisition of tower semiconductor