app mysql sqlite_实例讲解Android App使用自带的SQLite数据库的基本方法

SQLite数据库是android系统内嵌的数据库,小巧强大,能够满足大多数SQL语句的处理工作,而SQLite数据库仅仅是个文件而已。虽然SQLite的有点很多,但并不是如同PC端的mysql般强大,而且android系统中不允许通过JDBC操作远程数据库,所以只能通过webservice等手段于php、servlet交互获取数据。

基础SQLiteDatabase类,代表了一个数据库对象,通过SQLiteDatabase来操作管理数据库。

一些基本的用法:

static  SQLiteDatabase openDatabase(String path,SQLiteDatabase.CUrsorFactory factory,int flag);

static SQLiteDatabase openOrCreateDatabase(File file,SQLiteDatabase.CursorFactory factory);

static SQLiteDatabase openOrCreateDatabase(String path,SQLiteDatabse.CursorFactory factory);

通过这些静态方法可以很方便的打开和新建一个数据库。

execSQL(String sql,Object[] bindArgs)

execSQL(String sql)

rawQuery(String sql,String[] selectionArgs);

beginTransaction()

endTransaction()

这些函数可以完成SQL功能,对于查询出来的结果是用Cursor表示的,类似于JDBC中的ResultSet类,在这些类中通过方法move(int offset)、moveToFirst()、moveToLast()、moveToNext()、moveToPosition(int position)、moveToPrivious()获取需要的结果行。

下面通过一个实例来说明一下SQLiteDatabase的基本使用:

main.xml:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".Main" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:text="key" />

android:id="@+id/keys"

android:layout_width="100sp"

android:layout_height="wrap_content" />

android:l

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值