前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android开发工程师文集-1 小时学会SQLite

Android开发工程师文集-1 小时学会SQLite

作者头像
达达前端
发布2019-07-03 12:18:34
4070
发布2019-07-03 12:18:34
举报
文章被收录于专栏:达达前端达达前端

前言

大家好,我是 Vic,今天给大家带来Android开发工程师文集-1 小时学会SQLite的概述,希望你们喜欢

内容

  • 什么是Sqlite: 效率高,开源,小型,程序驱动,支持事务操作,无数据类型,可嵌入的关系型数据库 独立的,跨平台的,代码量少,简单易用

创建表语句

代码语言:javascript
复制
create table student(_id Integer primary key, name varchar(10), age Integer not null);

删除表

代码语言:javascript
复制
drop table student;

插入数据

代码语言:javascript
复制
Insert into 表名(字段列表) values (值列表);
insert into student(_id,age) values(1,17);
insert into student values(1,"vic",17);

修改数据

代码语言:javascript
复制
update student set name="vic",age=17 where _id=1;

更新数据

代码语言:javascript
复制
Update 表名 set 字段=值 列表 更新的条件

删除数据

代码语言:javascript
复制
delete from 表名 [删除条件];
delete from student where _id=1;

查询语句

代码语言:javascript
复制
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()

代码语言:javascript
复制
<?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>
代码语言:javascript
复制
//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();
 }
}
代码语言:javascript
复制
//创建一个类
//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--");
 }
}
代码语言:javascript
复制
//创建库表
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";
代码语言:javascript
复制
public class DbManger{
 private static MySqliteHelper helper;
 public static MySqliteHelper getIntance(Context context){
   if(helper == null){
     helper=new MySqliteHelper(content);
   }
   return hepler;
 }
}
代码语言:javascript
复制
<?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>
代码语言:javascript
复制
//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;
 }
}
代码语言:javascript
复制
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);
  }
 }
}
代码语言:javascript
复制
<?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>
代码语言:javascript
复制
//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;
 }
}
代码语言:javascript
复制
<?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>
代码语言:javascript
复制
//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;
 }
}
代码语言:javascript
复制
<?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>
代码语言:javascript
复制
//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;
 }
}
代码语言:javascript
复制
<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"/>
代码语言:javascript
复制
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;
   }
}
代码语言:javascript
复制
<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"/>
代码语言:javascript
复制
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;
}
代码语言:javascript
复制
 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知识点,有兴趣可以继续关注
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.04.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
    • 内容
      • 内容
        • 要点
          • 总结
          相关产品与服务
          数据库
          云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档