当前位置:网站首页>SQlife(数据库)
SQlife(数据库)
2022-07-27 17:02:00 【Ashurol】
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//每个程序都有自己的数据库 默认情况下是各自互相不干扰
//创建一个数据库 并且打开
SQLiteDatabase db=openOrCreateDatabase("user.db", MODE_PRIVATE, null);
db.execSQL("create table if not exists usertb(_id integer primary key autoincrement, name text not null, age integer not null,sex text not null)");//在数据库里建表
/*
* 第一种往表里增加数据的方法
*/
// db.execSQL("insert into usertb(name,sex,age) values('张三','男',66)");
// db.execSQL("insert into usertb(name,sex,age) values('李四','男',22)");
// db.execSQL("insert into usertb(name,sex,age) values('王五','男',33)");
// db.execSQL("insert into usertb(name,sex,age) values('老王他老婆','女',45)");<img src="https://img-blog.csdn.net/20150719011908954?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
/*
* 第二种往表里增加数据的方法
*/
ContentValues values=new ContentValues();/*ContentValues类和Hashtable比较类似,
它也是负责存储一些键值对,
但是它存储的键值对当中的键是一个String类型,而值都是基本类型。*/
values.put("name", "张三");//语句将列名和对应的列值放置到Values里边。
values.put("sex", "男");
values.put("age", 21);
long rowId=db.insert("usertb", null, values);//语句负责插入一条新的纪录,如果插入成功则会返回这条记录的id,如果插入失败会返回-1。
values.clear();//清除values里的内容,也可以重新创建ContentValues对象
values.put("name", "王二");
values.put("sex", "男");
values.put("age", 29);
db.insert("stutb", null, values);
values.clear();values.put("name", "小脑");
values.put("sex", "女");
values.put("age", 25);
db.insert("stutb", null, values);
values.clear();values.put("name", "小鱼");
values.put("sex", "男");
values.put("age", 18);
db.insert("stutb", null, values);
values.clear();
values.put("name", "田湾");
values.put("sex", "男");
values.put("age", 23);
db.insert("stutb", null, values);
values.clear();
values.put("name", "张三封");
values.put("sex", "男");
values.put("age", 55);
db.insert("stutb", null, values);
values.clear();<img src="https://img-blog.csdn.net/20150719011933912?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
/*
* 修改表里的信息
*/
values.put("sex", "女");
db.update("stutb", values, "_id>?", new String[]{"2"});//将全部id>2的人的性别改成女
/*
* 删除数据库信息
*/
db.delete("stutb", "name like ?", new String[]{"%二%"});//删除所有名字中带有二的人
/*
* 查询方法1db.rawQuery()
*/
// Cursor c=db.rawQuery("select * from usertb", null);//查询表内所有数据
// if(c!=null)
// {
// while(c.moveToNext())
// {
// Log.i("info","_id:"+c.getInt(c.getColumnIndex("_id")));
// Log.i("info","name:"+c.getString(c.getColumnIndex("name")));
// Log.i("info","name:"+c.getString(c.getColumnIndex("sex")));
// Log.i("info","name:"+c.getString(c.getColumnIndex("age")));
// Log.i("info","---------");
// }
// c.close();
// }
// db.close();
/*
* 查询方法2db.query()
*/
Cursor c= db.query("stutb", null, "_id>?", new String[]{"0"}, null, null, "_id");//_id代表排序方式,也可以用name这种排序方式
if (c!=null) {
String [] columns= c.getColumnNames();//getColumnNames()——返回所有列的名字,getColumnCount()-返回所以列的总数
while (c.moveToNext()) {
for (String columnName : columns) //增强循环,将数组columns里的数依次取出赋值给columnName
{
Log.i("info", c.getString(c.getColumnIndex(columnName)));
}
}
c.close();
}
db.close();
}
}//用SQLiteOpenHelper类建数据库
<pre name="code" class="java">public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBOpenHelper helper = new DBOpenHelper(MainActivity.this, "stu.db");
// helper.getReadableDatabase();//获取一个只读的数据库 只能查询 不能写入 不能更新
SQLiteDatabase db = helper.getWritableDatabase();
// db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy)
Cursor c = db.rawQuery("select * from stutb", null);
if (c!=null) {
String [] cols = c.getColumnNames();//得到所以列名
while (c.moveToNext()) {
for (String ColumnName : cols) //将cols数组里的元素赋值给ColumnName
{
Log.i("info", ColumnName+":"+c.getString(c.getColumnIndex(ColumnName)));
}
}
c.close();
}
db.close();
}
}
//SQLiteOpenHelper类<pre name="code" class="java">public class DBOpenHelper extends SQLiteOpenHelper{
public DBOpenHelper(Context context, String name) {
super(context, name, null, 1);
// TODO Auto-generated constructor stub
}
public DBOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
//首次创建数据库的时候调用 一般是建库 建表的操作
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists stutb(_id integer primary key autoincrement,name text not null,sex text not null,age integer not null)");
db.execSQL("insert into stutb(name,sex,age)values('张三','女',18)");
}
@Override//当数据库的版本发生变化的时候 会自动执行
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}

边栏推荐
- Introduction to several wireless protocols
- AutoCompleteTextView(输入框预匹配)
- rxbinding
- PyTorch报CUDA error: no kernel image is available for execution on the device 错误
- The first in the field of mobile phone chip design in the world! Ziguang zhanrui won the international certification of tmmi4
- 【深度学习基础知识 - 39】BN、LN、WN的比较
- c语言:12、gdb工具调试c程序
- MarqueeTextview(跑马灯)
- 估值超156亿元!华勤通讯完成10亿元B轮融资!高通创投、英特尔资本领投
- Debian recaptured the "debian.community" domain name, but it's still not good to stop and rest
猜你喜欢

GestureDetector(手势识别)

ToggleButton(按钮开关)

应用程序池已被禁用

c语言:13、指针与内存

C language: 10. Input stream, output stream, error stream

C language: clion debugging method

Under the heat wave of Web3.0, the ecological shock of Mensa struck

C language: 13. Pointer and memory

Chinese character search Pinyin wechat applet project source code

Original pw4203 step-down 1-3 lithium battery charging chip
随机推荐
OPPO发布首款AR眼镜,宣布未来3年投入500亿进行研发
【深度学习基础知识 - 45】机器学习中常用的距离计算方法
[basic knowledge of deep learning - 47] Bayesian networks and naive Bayes
C language: 8. Makefile preparation
Golang sets the domestic image, vscode configures the golang development environment, and vscode debugs the golang code
go-zero单体服务使用泛型简化注册Handler路由
c语言:9、main函数中的return
Summary of APP launch in vivo application market
Uncover the mystery of Qualcomm ultrasonic fingerprint being "cracked by film"
嵌入式C语言对次数固定的循环的优化
[basic knowledge of deep learning - 49] kmeans
27、golang基础-互斥锁、读写锁
PyTorch报CUDA error: no kernel image is available for execution on the device 错误
C language: 12. GDB tool debugging C program
应用程序池已被禁用
To create a MySQL data source resource group, you must choose to create a new exclusive data integration resource group? Or use a common resource group? thank you
Tab control of MFC advanced control (CTabCtrl)
GestureOverlayView(手势识别2)
DatePicker(日期选择器)与TimePicker(时间选择器)
5W bonus pool / for colleges and universities, 2022 legal science and technology innovation competition is in progress