Android SqlLite 学习

news/2024/7/5 6:23:39

今天学习了Android SqlLite 一些知识,记录下

一、使用SQLiteDatabase方法一:
1.创建数据库

SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase("databasename.db",MODE_PRIVATE,null); 

2.创建表

String sql="create table stu(_id Integer primary key autoincrement,name varchar(20),grade integer)";  
db.execSQL(sql); 

含义:创建表 table stu,其中标的_id是编号,是主键,可自动增加,类型为整形,其他连个属性是name和grade 一个char型,一个整形
更新、查询等操作也可以使用sql语句通过execSQL 去操作,也可以使用下面函数
3.插入

ContentValues  values=new ContentValues();  
values.put("name", "Zhang");  
values.put("grade", 5);  
db.insert("stu", null, values); 

4.删除

db.delete(stu, “name=? and tel=?”, new String[]{“Zhang”,”5”});

5.更新
函数操作

    ContentValues  values=new ContentValues();    
    values.put("name", "Zhang");  
    values.put("grade", 5);  
    db.update("stu", values, "_id=?", new String[]{"1"}); 

语句操作

String sql="update stu set name=? where _id=?";  
db.execSQL(sql, new Object[]{"Zhang",1}); 

6.查询

Cursor cs=db.query("stu", new String[]{"*"}, "_id=?", new String[]{1+""}, null, null, null);  

while(cs.moveToNext())  
{  
      String name=cs.getString(cs.getColumnIndex("name"));  
      int grade=cs.getInt(2);  
      int _id=cs.getInt(cs.getColumnIndex("_id"));  
}  

二、继承SQLiteOpenHelper类
继承SQLiteOpenHelper类,并重写onCreate()和onUpgrade()
1.构造函数中创建数据库

    private static final String DATA_BASE_NAME = "book.db";
    private static final int DATE_BASE_VERSION = 1;
    public DbOpenHelper(Context context) {
        super(context, DATA_BASE_NAME, null, DATE_BASE_VERSION);
    }

context是对应的上下文,DATA_BASE_NAME是数据库的名称,DATE_BASE_VERSION是数据库版本号

2.创建表

 private final  String CREATE_STUDENT_TABLE = "create table " + STUDENT_TABLE_NAME + "(_id integer primary key autoincrement,userName text,sex text,grade text)";
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_STUDENT_TABLE);
    }

onCreate中创建数据库表

3.增删改查

 sqLiteDatabase = new DbOpenHelper(context).getWritableDatabase();
 sqLiteDatabase.beginTransaction();
 ContentValues contentValues = new ContentValues();
 contentValues.put("bookName", "Computer");
 sqLiteDatabase.insert(DbOpenHelper.BOOK_TABLE_NAME, null, contentValues);
 sqLiteDatabase.setTransactionSuccessful();
 sqLiteDatabase.endTransaction();

三、URI学习

1.创建URI

    public static final String AUTHORITY = "com.example.admin.mycontenter.BookProvider";
    public static final int BOOK_URI_CODE = 0;
    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI(AUTHORITY, DbOpenHelper.BOOK_TABLE_NAME, BOOK_URI_CODE);

创建 URI 链接,AUTHORITY链接地址,DbOpenHelper.BOOK_TABLE_NAME链接地址后面添加的名字,如content//:com.example.admin.mycontenter.BookProvider/book;
BOOK_URI_CODE为根据链接查找是对应的ID号;

当URI没有添加时,得到的cursor 为空

    Uri uri = Uri.parse("content://com.example.admin.mycontenter.BookProvider/grade");
    Log.e("MainActivity", "test uri" );
    Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);

    if(cursor == null){
        Log.e("MainActivity", "test cursor is null" );
    }

    if(cursor != null){
        if(cursor.moveToNext()){
            Log.e("MainActivity", "test cursor next is not null" );
        }
        Log.e("MainActivity", "test cursor close" );
        cursor.close();
    }

2.ContentProvider解析URI
ContentProvider可以根据URI进行查询、插入等操作,其中getContentResolver()可以得到对应context环境的ContentProvider对象,如自己实现的继承ContentProvider的类的类

3.继承ContentProvider 类需要AndroidManifest中添加

<provider
    android:authorities="com.example.admin.mycontenter.studentProvider"
    android:name=".studentProvider"/>

今天测试发现该类会在Activity类前进行执行,并调用该类的onCreate函数


http://www.niftyadmin.cn/n/1390319.html

相关文章

DAO设置模式

区分&#xff1a; J2EE的组件层次&#xff1a; 客户端---表示层---业务层---数据层---数据库---数据库 DAO属于数据层的操作&#xff0c;即&#xff1a;在DAO中封装了一个表在项目中的多有的操作 举例&#xff1a; person表 id name password age 定义各种操作: 在java中只有通…

中继器的使用

目的显示&#xff0c;姓名对应的年龄 1、 2、设定数据源 3、拖入2个Label组件&#xff0c;分别命名为lbl_name、lal_age&#xff0c;显示姓名、年龄。 4、把数据、和视图练习起来。相当于MVC的C,控制跳转。 最后&#xff0c;F5运行 本文转载自SharkBin博客园博客&#xff0c;原…

用JS有效解决移动web浏览器中HTML元素的overflow:scroll滚动属性失效问题

web移动平台前端UI开发工作&#xff0c;兼容问题超多&#xff0c;今儿又遇到一个。产品方要求在某固定尺寸容器内显示内容&#xff0c;但内容条数未知&#xff1b;如果条数过多&#xff0c;容器显示滚动条。这鸟需求按说是So easy&#xff0c;容器设死宽、高&#xff0c;CSS加属…

Flex练习:写一个五子连珠游戏 Five And More

注意&#xff1a;火狐和谷歌浏览器看不到效果,是因为在博客中&#xff0c;一些脚本无法配置&#xff0c;和Flex的兼容性无关 玩法&#xff1a;先选择棋盘中的一个棋子&#xff0c;然后点击你想移动到的空格&#xff0c;五个同样颜色在一条线上就可以消去。玩一把试试吧&#xf…

Android Timber 学习

1.AS中添加依赖项 implementation "com.jakewharton.timber:timber:4.5.1" 2.OnCreate中注册 if (BuildConfig.DEBUG) {Timber.plant(new Timber.DebugTree()); } else {Timber.plant(new CrashReportingTree()); }添加类 private static class CrashReportingTre…

Zabbix代理proxy架构搭建

众所周知&#xff0c;Zabbix是一个基于Web界面的提供分布式系统监视以及网络监视功能的企业级开源解决方案。Zabbix能监视各种网络尝试&#xff0c;保证服务器系统的安全运营&#xff1b;并提供灵活的通知机制以让系统工程师快速定位/解决存在的问题。当我们的监控的服务器增多…

LINUX-vbird

http://www.linux.org.tw/ftp://ftp.nsysu.edu.tw/服务器&#xff1a;NAT:SAMBA:Mail:Web:DHCP:Proxy:FTP基本指令显示日期的指令&#xff1a;date显示日历的指令&#xff1a;calcal [month] [year]简单好用的计算器&#xff1a;bcbc默认仅输出整数&#xff0c;如果要输出小数点…

代码设置SPList权限(添加SPGroup)

使用代码为SPList添加用户组&#xff0c;并且设置用户组的权限 代码实现 using (SPSite mySPSite new SPSite(<SPSiteURL>)) { SPWeb mySPWeb mySPSite.RootWeb; string spGroupName1 "mySPGroup1"; //添加的用户必须是Domain中存在的用户 string spUserNa…