SQLite数据库
什么是SQLite?
含义:
SQLite是Android 系统中集成的轻量级的数据库
特点:
1.轻量级 只用一个动态的库, 是以单个文件的形式进行存取
2.跨平台 支持多个操作系统
3.零配置 无需安装, 直接使用
4.嵌入式 内嵌到手机中
可存放以下类型的数据:
NULL 空值
INTEGER 整型(不用int)
VARCHAR 可变长度的字符数据
TEXT 文本字符串
BOOLEAN 布尔
DATE 时期
SQLite中两个主要的类是什么
一. SQLiteOpenHelper 数据库的帮助类
二. SQLiteDatabase 数据库的操作类
SQLite两个类分别的作用
一. SQLiteOpenHelper 数据库的帮助类
SQLiteOpenHelper是数据库的帮助类, 用于数据库的创建和版本更新
使用方法:
(1)定义一个类, 继承SQLiteOpenHelper
(2)重写构造方法 :提供数据库的基本信息 : 上下文对象,数据库名称,Null,数据库的版本号
(3)重写父类的两个方法:
onCreate(): onUpgrade()
二. SQLiteDatabase 数据库的操作类
SQLiteDatabase 是数据库的操作类, 操作数据库: 执行sql语句/增/删/改/查
使用时首先需要通过SQLiteOpenHelper获取SQLiteDatabase对象
SQLite的增删改查
增
原生方式:
String sql = "insert into user(name,adress) values(?,?)";
db.execSQL(sql,new Object[]{"成高薪","北京"});
封装方法:
ContentValues contentValues = new ContentValues();
contentValues.put("name","成宗朋");
contentValues.put("address","唐山");
db.insert("user",null, contentValues);
事务批量处理:
db.beginTransaction();//开启事务
try {
for (int i=0;i<1000;i++) {
db.execSQL("insert into user(name,adress) values(?,?) ");
}
db.setTransactionSuccessful();//成功
}catch (Exception e){
e.printStackTrace();
}finally {
db.endTransaction();//结束事务
db.close();//关闭
}
删
原生方式:
Cursor cursor = db.query("user", null, null, null, null, null, null);
if(cursor!=null){
while(cursor.moveToNext()){
String address = cursor.getString(cursor.getColumnIndex("address"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
Toast.makeText(this, id+"----"+name+"----"+address, Toast.LENGTH_SHORT).show();
}
}
封装方法:
//方法2
String sql = "select * from user";
db.execSQL(sql);
改
原生方式:
ContentValues contentValues = new ContentValues();
contentValues.put("adress","日本");
db.update("user",contentValues,"id = ?",new String[]{"2"});
封装方法:
String sql = "update user set age = ? where _id=?";
db.execSQL(sql,new Object[]{user.getAge(),user.get_id()});
查
原生方式:
String sql = "delete from user where id = ?";
db.execSQL(sql,new Object[]{2});
封装方法:
db.delete("user","id = ?",new String[]{"2"});
总
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:text="插入"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="click"
/>
<Button
android:text="查询"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="click2"
/>
<Button
android:text="修改"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="click3"
/>
<Button
android:text="删除"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:onClick="click4"
/>
</LinearLayout>
Activity
package com.example.study_day09;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.study_day09.SQL.MySql;
public class MainActivity extends Activity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySql mySql = new MySql(this, "user.db", null, 1);
db = mySql.getReadableDatabase();
}
//插入
public void click(View view) {
// ContentValues contentValues = new ContentValues();
// contentValues.put("name","成宗朋");
// contentValues.put("address","唐山");
// db.insert("user",null, contentValues);
// String sql = "insert into user(name,adress) values(?,?)";
// db.execSQL(sql,new Object[]{"成高薪","北京"});
db.beginTransaction();//开启事务
try {
for (int i=0;i<1000;i++) {
db.execSQL("insert into user(name,adress) values(?,?) ");
}
db.setTransactionSuccessful();//成功
}catch (Exception e){
e.printStackTrace();
}finally {
db.endTransaction();//结束事务
db.close();//关闭
}
}
//查询
public void click2(View view) {
//方法一:
Cursor cursor = db.query("user", null, null, null, null, null, null);
if(cursor!=null){
while(cursor.moveToNext()){
String address = cursor.getString(cursor.getColumnIndex("address"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
Toast.makeText(this, id+"----"+name+"----"+address, Toast.LENGTH_SHORT).show();
}
}
//方法2
String sql = "select * from user";
db.execSQL(sql);
}
//修改
public void click3(View view) {
// String sql = "update user set age = ? where _id=?";
// db.execSQL(sql,new Object[]{user.getAge(),user.get_id()});
//方式2
ContentValues contentValues = new ContentValues();
contentValues.put("adress","日本");
db.update("user",contentValues,"id = ?",new String[]{"2"});
}
//删除
public void click4(View view) {
String sql = "delete from user where id = ?";
db.execSQL(sql,new Object[]{2});
db.delete("user","id = ?",new String[]{"2"});
}
}