Android内置SQLite的使用详细介绍


当前第2页 返回上一页

 table:表名。相当于select语句from关键字后面的部分。如果是多表联合查询,可以用逗号将两个表名分开。

columns:要查询出来的列名。相当于select语句select关键字后面的部分。

selection:查询条件子句,相当于select语句where关键字后面的部分,在条件子句允许使用占位符“?”

selectionArgs:对应于selection语句中占位符的值,值在数组中的位置与占位符在语句中的位置必须一致,否则就 会有异常。

groupBy:相当于select语句group by关键字后面的部分

 having:相当于select语句having关键字后面的部分

orderBy:相当于select语句order by关键字后面的部分,如:personid desc, age asc;

limit:指定偏移量和获取的记录数,相当于select语句limit关键字后面的部分。

(2)查询时用到的方法——方法2

rawQuery(String sql,String[ ] selectionArgs)

方法各参数的含义:

sql :实现查询的sql语句,例如: select * from stu_info where sno=?

 selectionArgs:是?条件参数,如果?这个内占位符容为null的话就表示把所有的学号的学生都查出来

(3)查询结果处理

(4)总结

四、修改数据  

1、界面效果图

2、布局界面 activity_ third.xml

<?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"
     android:orientation="vertical"
     android:background="@drawable/modifybg"
     tools:context=".ThirdActivity">
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="信息修改页面"
         android:textSize="30sp"
         android:textStyle="bold"
         android:textColor="#000000"
         android:layout_gravity="center"
         android:layout_margin="80dp"/>
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="horizontal"
         android:layout_marginBottom="30dp">
         <EditText
             android:id="@+id/editText_threeinputsno"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:hint="请输入要查询的学号"
             android:textSize="25sp"/>
         <Button
             android:id="@+id/button_threequery"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:text="查询"
             android:textSize="25sp"/>
     </LinearLayout>
     <EditText
         android:id="@+id/editText_threesno"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="学号"
         android:textSize="25sp"/>
     <EditText
         android:id="@+id/editText_threename"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="姓名"
         android:textSize="25sp"/>
     <EditText
         android:id="@+id/editText_threedep"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="所属系部"
         android:textSize="25sp"/>
     <Button
         android:id="@+id/button_threemodify"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="修改"
         android:textSize="25sp"
         android:layout_gravity="right"
         android:layout_marginTop="30dp"/>
     <Button
         android:id="@+id/button_threenext"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="下一页"
         android:textSize="25sp"
         android:layout_gravity="right"/>
</LinearLayout>

3、类文件 ThirdActivity.java

public class ThirdActivity extends AppCompatActivity {
//定义对象
     EditText edit_threeinputsno,edit_threesno,edit_threename,edit_threedep;
     Button btn_threequery,btn_threemodify,btn_threenext;
     MyDBOpenHelper mhelper;//定义一个数据库帮助类对象
     SQLiteDatabase db;//定义一个操作的数据库的类对象
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_third);
         //1 控件初始化
         initView();
         //2 查询按钮功能的实现
         btnQuery();
         //3 修改按钮功能的实现
         btnModify();
         //4 下一步按钮功能的实现
         btnNext();
     }
     //1 控件初始化-------------代码
     private void initView() {
         edit_threeinputsno=findViewById(R.id.editText_threeinputsno);
         edit_threesno=findViewById(R.id.editText_threesno);
         edit_threename=findViewById(R.id.editText_threename);
         edit_threedep=findViewById(R.id.editText_threedep);
         btn_threequery=findViewById(R.id.button_threequery);
         btn_threemodify=findViewById(R.id.button_threemodify);
         btn_threenext=findViewById(R.id.button_threenext);
         mhelper=new MyDBOpenHelper(ThirdActivity.this);//实例化数据库帮助类对象
         db= mhelper.getWritableDatabase();//获取数据库的读写权限
     }
     //2 查询按钮功能的实现
     private void btnQuery() {
         btn_threequery.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //先查询显示,再修改。参数(String sql,String[ ] selectionArgs)
                 Cursor cursor=db.rawQuery("select * from stu_info where sno=?",new String[]{edit_threeinputsno.getText().toString()});
                 if(cursor.getCount()!=0){
                     Toast.makeText(ThirdActivity.this,"查询成功",Toast.LENGTH_SHORT).show();
                     while(cursor.moveToNext()){
                         String mysno=cursor.getString(cursor.getColumnIndex("sno"));
                         String myname=cursor.getString(cursor.getColumnIndex("name"));
                         String mydep=cursor.getString(cursor.getColumnIndex("deparment"));
                         edit_threesno.setText(mysno);
                         edit_threename.setText(myname);
                         edit_threedep.setText(mydep);
                     }
                 }else{
                     Toast.makeText(ThirdActivity.this,"没有查询到该学号的学生",Toast.LENGTH_SHORT).show();
                     edit_threesno.setText("");
                     edit_threename.setText("");
                     edit_threedep.setText("");
                 }
             }
         });
     }
     //3 修改按钮功能的实现---------代码
     private void btnModify() {
         btn_threemodify.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //修改数据代码如何写呢?参数:(表名,ContentValues 对象,更新的条件,条件的参数)
                 ContentValues values=new ContentValues();
 
                 values.put("deparment",edit_threedep.getText().toString());
                 db.update("stu_info",values,"sno=?",new String[]{edit_threesno.getText().toString()});
                 Toast.makeText(ThirdActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
             }
         });
     }
     //4 下一页按钮功能的实现------代码
     private void btnNext() {
         btn_threenext.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Intent intent=new Intent(ThirdActivity.this,FourActivity.class);
                 startActivity(intent);
                 finish();
             }
         });
     }
}

4、代码讲解

(1)update()方法的四个参数

update(String table,ContentValues values,String whereClause,String[ ] whereArgs)

  1、第一个参数表名;

  2、第二个参数是ContentValues对象,要把更新的数据在这里组装进去;

  3、第三个参数是更新的条件  

  4、第四个参数是条件的参数

(2)总结

五、删除数据  

1、界面效果图

2、布局界面 activity_ four.xml

<?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"
     android:orientation="vertical"
     android:background="@drawable/deletebg"
     tools:context=".FourActivity">
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="信息删除页面"
         android:textSize="30sp"
         android:textStyle="bold"
         android:textColor="#000000"
         android:layout_gravity="center"
         android:layout_margin="80dp"/>
     <EditText
         android:id="@+id/editText_foursno"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:hint="请输入要删除的学号"
         android:textSize="25sp"/>
     <Button
         android:id="@+id/button_fourdelete"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="删除"
         android:textSize="25sp"
         android:layout_gravity="right"/>
</LinearLayout>

3、类文件 FourActivity.java

public class FourActivity extends AppCompatActivity {
//定义对象
     EditText edit_foursno;
     Button btn_fourdelete;
     MyDBOpenHelper mhelper;//定义一个数据库帮助类对象
     SQLiteDatabase db;//定义一个操作的数据库的类对象
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_four);
         //1 控件初始化
         initView();
         //2 删除按钮功能的实现
         btnDelete();
     }
     //1 控件初始化----------代码
     private void initView() {
         edit_foursno=findViewById(R.id.editText_foursno);
         btn_fourdelete=findViewById(R.id.button_fourdelete);
         mhelper=new MyDBOpenHelper(FourActivity.this);//实例化数据库帮助类对象
         db=mhelper.getWritableDatabase();//获取数据库的读写权限
     }
     //2 删除按钮功能的实现-----代码
     private void btnDelete() {
         btn_fourdelete.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //怎么样删除呢?参数:(表名,删除的条件,条件的参数)
                 db.delete("stu_info","sno=?",new String[]{edit_foursno.getText().toString()});
                 Toast.makeText(FourActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
             }
         });
     }
}

4、代码讲解

(1)delete()方法的三个参数

delete(String table,String whereClause,String[ ] whereArgs)

  1、第一个参数:表名;

  2、第二个参数:删除的条件  

  3、第三个参数:条件的参数

(2)总结

到此这篇关于Android内置SQLite的使用详细介绍的文章就介绍到这了,更多相关Android SQLite使用内容请搜索


标签:SQLite

返回前面的内容

相关阅读 >>

nginx可视化配置工具nginxwebui的使用

Sqlite中重置自动编号列的方法

android Sqlite命令详解(基本命令)

Sqlite 入门教程一 基本控制台(终端)命令

Sqlite数据库管理相关命令的使用介绍

Sqlite5-使用python来读写数据库

Sqlite字符串比较时的大小写问题解决方法

迅雷高速通道怎么破解使用?迅雷高速通道无限使用破解教程详解

Sqlitestudio优雅调试android手机数据库Sqlite(推荐)

.net6跨服务器联表查询操作mysql、oracle、sqlserver等相互联表

更多相关阅读请进入《Sqlite》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...