前言 大家好,給大家帶來 的概述,希望你們喜歡 內容 什麼是Sqlite: 效率高,開源,小型,程式驅動,支持事務操作,無數據類型,可嵌入的關係型資料庫 獨立的,跨平臺的,代碼量少,簡單易用 創建表語句 刪除表 插入數據 修改數據 更新數據 刪除數據 查詢語句 內容 創建資料庫 實現資料庫中的增刪改 ...
前言
大家好,給大家帶來Android開發工程師文集-1 小時學會SQLite
的概述,希望你們喜歡
內容
- 什麼是Sqlite:
效率高,開源,小型,程式驅動,支持事務操作,無數據類型,可嵌入的關係型資料庫
獨立的,跨平臺的,代碼量少,簡單易用
創建表語句
create table student(_id Integer primary key, name varchar(10), age Integer not null);
刪除表
drop table student;
插入數據
Insert into 表名(欄位列表) values (值列表);
insert into student(_id,age) values(1,17);
insert into student values(1,"vic",17);
修改數據
update student set name="vic",age=17 where _id=1;
更新數據
Update 表名 set 欄位=值 列表 更新的條件
刪除數據
delete from 表名 [刪除條件];
delete from student where _id=1;
查詢語句
select 欄位名 from 表名稱 [查詢條件];
select 列名稱 from 表名稱 where 條件;
group by 分組的欄位 having 篩選條件 order by 排序欄位;
select * from student;
select _id from student;
select * from student where _id=1;
select * from student where _id=1 and age>17;
select * from student where age like "%1%";
select * from student where age>17 order by _id=1;
內容
- 創建資料庫
- 實現資料庫中的增刪改查
要點
SQLiteOpenHelper
,onCreate()
,onUpgrade()
,onOpen()
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="創建"
android:onClick="create"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
}
//創建一個類
//sqliteOpenHelper
//提供了onCreate()和onUpgrade()與onOpen()
public class MySqliteHelper extends SQLiteOpenHelper{
public MySqliteHelper(Context context, String name, SQLitebase.CursorFactory factory, int version){
super(context,name,factory,version);
}
public MySqliteHelper(Context context){
super(context,Constant.DATABASE_NAME,null,Constant.DATABASE_VERSION);
}
//資料庫創建時回調
@Override
public void onCreate(SQLiteDatabase db){
Log.i("tag","--onCreate--");
//String sql="create table student(_id Integer primary key,name verchar(10),age Integer)";
String sql = "create table "+Constant.TABLE_NAME+"("+Constant._ID+" Integer primary key,"+Constant.NAME+" varchar(10),"+Constant.AGE+" Integer)";
db.execSQL(sql);//執行資料庫語句
}
//資料庫更新
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion, int newVersion){
Log.i("tag","--onUpgrade--");
}
//資料庫打開
@Override
public void onOpen(SQLiteDatabase db){
super.onOpen(db);
Log.i("tag","--onOpen--");
}
}
//創建庫表
public class Contant{
public static final String DATABASE_NAME=“info.db”;//資料庫名稱
public static final int DATABASE_VERSION=1;//資料庫的版本號
public static final String TABLE_NAME="student";//表名
//用這裡表示
public static final String _ID="_id";
public static final String _NAME="name";
public static final String AGE="age";
public class DbManger{
private static MySqliteHelper helper;
public static MySqliteHelper getIntance(Context context){
if(helper == null){
helper=new MySqliteHelper(content);
}
return hepler;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="創建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
}
}
public class DbManger{
private static MySqliteHelper helper;
public static MySqliteHelper getIntance(Context context){
if(helper == null){
helper=new MySqliteHelper(content);
}
return hepler;
}
public static void execSQL(SQLiteDatabase db,String sql){
if(db!=null){
if(sql!=null && !"".equals(sql)){
db.execSQL(sql);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="創建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
case R.id.btn_update:
db=helper.getWritableDatabase();
String updateSql="update "+Constant.TABLE_NAME"+" set "+Contant.NAME+"='vic2' where "+Contant._ID+"=1";
DbManger.execSQL(db,updateSql);
db.close();
break;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="創建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
case R.id.btn_update:
db=helper.getWritableDatabase();
String updateSql="update "+Constant.TABLE_NAME"+" set "+Contant.NAME+"='vic2' where "+Contant._ID+"=1";
DbManger.execSQL(db,updateSql);
db.close();
break;
case R.id.btn_delete:
db=helper.getWritableDatabase();
String delSql="delete from "+Constant.TABLE_NAME+" where "+Constant._ID+"=2";
DbManger.execSQL(db,delSql);
db.close();
break;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_bg"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="創建"
android:onClick="create"
android:background="#000000"/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除數據"
android:onClick="click"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_insertApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入數據"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
</LinearLayout>
//MainActivity.java
public class MainActivity extends AppCompatActivity {
private MySqliteHelper helper;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = DbManger.getIntance(this);
}
public void createdb(View view){
SQLiteDatabase db=helper.getWritableDatable();
}
public void onClick(View view){
switch(view.getId()){
case R.id.btn_insertApi:
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Constant._ID,3);
values.put(Constant.NAME,"vic");
values.put(Constant.AGE,17);
long result=db.insert(Constant.TABLE_NAME,null,values);
if(result>0){
Toast.makeText(MainActivity.this,"插入數據成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入數據失敗!",Toast.LENGTH_LONG).show();
}
db.close();
break;
}
}
public void click(View view){
switch(view.getId()){
case R.id.btn_insert:
SQLiteDatabase db=helper.getWritableDatabase();
String sql="insert into "+Constant.TABLE_NAME+" values(1,'vic',17)";
DbManger.execSQL(db,sql);
String sql2="insert into "+Constant.TABLE_NAME+" values(2,'vic',23)";
DbManger.execSQL(db,sql2);
db.close();
break;
case R.id.btn_update:
db=helper.getWritableDatabase();
String updateSql="update "+Constant.TABLE_NAME"+" set "+Contant.NAME+"='vic2' where "+Contant._ID+"=1";
DbManger.execSQL(db,updateSql);
db.close();
break;
case R.id.btn_delete:
db=helper.getWritableDatabase();
String delSql="delete from "+Constant.TABLE_NAME+" where "+Constant._ID+"=2";
DbManger.execSQL(db,delSql);
db.close();
break;
}
}
<Button
android:id="@+id/btn_insertApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入數據"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
<Button
android:id="@+id/btn_updateApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新數據"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
public void onClick(View view){
switch(view.getId()){
case R.id.btn_insertApi:
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Constant._ID,3);
values.put(Constant.NAME,"vic");
values.put(Constant.AGE,17);
long result=db.insert(Constant.TABLE_NAME,null,values);
if(result>0){
Toast.makeText(MainActivity.this,"插入數據成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入數據失敗!",Toast.LENGTH_LONG).show();
}
db.close();
break;
case R.id.btn_updateApi:
//String table 修改的數據表的名稱,ContentValues values,String whereClause 表示修改條件,String[] whereArgs
db=helper.getWritableDatabase();
//db.update(String table,ContentValues values,String whereClause,String[] whereArgs);
ContentValues cv=new ContentValues();
cv.put(Contant.NAME,"vic3");
int count=db.update(Constant.TABLE_NAME,cv,Contant.TABLE_NAME,cv,Constant._ID+"=?",new String[]{"3"});
if(count>0){
Toast.makeText(MainActivity.this,"插入數據成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入數據失敗!",Toast.LENGTH_LONG).show();
}
db.close();
break;
}
}
<Button
android:id="@+id/btn_updateApi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新數據"
android:onClick="onclick"
android:layout_marginTop="15dp"
android:background="#000000"/>
public void onClick(View view){
switch(view.getId()){
case R.id.btn_insertApi:
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(Constant._ID,3);
values.put(Constant.NAME,"vic");
values.put(Constant.AGE,17);
long result=db.insert(Constant.TABLE_NAME,null,values);
if(result>0){
Toast.makeText(MainActivity.this,"插入數據成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入數據失敗!",Toast.LENGTH_LONG).show();
}
db.close();
break;
case R.id.btn_updateApi:
//String table 修改的數據表的名稱,ContentValues values,String whereClause 表示修改條件,String[] whereArgs
db=helper.getWritableDatabase();
//db.update(String table,ContentValues values,String whereClause,String[] whereArgs);
ContentValues cv=new ContentValues();
cv.put(Contant.NAME,"vic3");
int count=db.update(Constant.TABLE_NAME,cv,Contant.TABLE_NAME,cv,Constant._ID+"=?",new String[]{"3"});
if(count>0){
Toast.makeText(MainActivity.this,"插入數據成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入數據失敗!",Toast.LENGTH_LONG).show();
}
db.close();
break;
case R.id.btn_deleteApi:
db=helper.getWritableDatabase();
//int count2=db.delete(String table,StringwhereClause,String[] whereArgs);
int count2=db.delete(Constant.TABLE_NAME,Constant._ID+"=?",new String[]{"1"});
if(count2>0){
Toast.makeText(MainActivity.this,"插入數據成功!",Toast.LENGTH_LONG).show();
}else{
Toast.makeText(MainActivity.this,"插入數據失敗!",Toast.LENGTH_LONG).show();
}
db.close();
break;
}
public int delete(String table,String whereClause,String[] whereArgs){
acquireReference();
try{
SQLiteStatement statement = new SQLiteStatement(this,"DELETE FROM "+table+(!TextUtils.isEmpty(whereClause) ? " WHERE "+whereClause : ""), whereArgs;
try{
return statement.executeUpdateDelete();
}finally{
statement.close();
}
}finally{
releaseReference();
}
總結
- 本文講了Android開發工程師文集-1 小時學會SQLite,如果您還有更好地理解,歡迎溝通
- 定位:分享
Android
&Java
知識點,有興趣可以繼續關註