Android游戏开发之SQLite数据库

Android游戏开发之SQLite数据库,第1张

  先介绍几个基本概念知识:

  什么是SQLite

  SQLite是一款轻量级数据库,它的设计目的是嵌入式,而且它占用的资源非常少,在嵌入式设备中,只需要几百KB!!!!!

  SQLite的特性:

  轻量级

  使用 SQLite 只需要带一个动态库,就可以享受它的全部功能,而且那个动态库的尺寸想当小。

  独立性

  SQLite 数据库的核心引擎不需要依赖第三方软件,也不需要所谓的“安装”。

  隔离性

  SQLite 数据库中所有的信息(比如表、视图、触发器等)都包含在一个文件夹内,方便管理和维护。

  跨平台

  SQLite 目前支持大部分 *** 作系统,不至电脑 *** 作系统更在众多的手机系统也是能够运行,比如:Android

  多语言接口

  SQLite 数据库支持多语言编程接口。

  安全性

  SQLite 数据库通过数据库级上的独占性和共享锁来实现独立事务处理。这意味着多个进程可以在同一时间从同一数据库读取数据,但只能有一个可以写入数据。

  优点:

  1.能存储较多的数据。

  2.能将数据库文件存放到SD卡中!

  什么是 SQLiteDatabase?

  一个 SQLiteDatabase 的实例代表了一个SQLite 的数据库,通过SQLiteDatabase 实例的一些方法,我们可以执行SQL 语句,对数 据库进行增、删、查、改的 *** 作。需要注意的是,数据库对于一个应用来说是私有的,并且在一个应用当中,数据库的名字也是惟一的。

  什么是 SQLiteOpenHelper ?

  根据这名字,我们可以看出这个类是一个辅助类。这个类主要生成一个数据库,并对数据库的版本进行管理。当在程序当中调用这个类的 方法getWritableDatabase(),或者getReadableDatabase()方法的时候,如果当时没有数据,那么Android 系统就会自动生成一 个数 据库。SQLiteOpenHelper 是一个抽象类,我们通常需要继承它,并且实现里边的3 个函数,

  什么是 ContentValues 类?

  ContentValues 类和Hashmap/Hashtable 比较类似,它也是负责存储一些名值对,但是它存储的名值对当中的名是一个

  String 类型,而值都是基本类型。

  什么是 Cursor ?

  Cursor 在Android 当中是一个非常有用的接口,通过Cursor 我们可以对从数据库查询出来的结果集进行随 机的读写访问。

  OK,基本知识就介绍到这里,下面开始上代码:还是按照我的一贯风格,代码中该解释的地方都已经在代码中及时注释和讲解了!

  顺便来张项目截图:

  

Android游戏开发之SQLite数据库,第2张

 

  先给出xml:

  view plaincopy to clipboardprint?

  ·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

  《?xml version=“1.0” encoding=“utf-8”?》

  《LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

  android:orientaTIon=“verTIcal” android:layout_width=“fill_parent”

  android:layout_height=“fill_parent”》

  《TextView android:layout_width=“fill_parent”

  android:layout_height=“wrap_content” android:text=“SQL 练习!(如果你使用的SD卡存储数据方式,为了保证正常 *** 作,请你先点击创建一张表然后再 *** 作)”

  android:textSize=“20sp” android:textColor=“#ff0000” android:id=“@+id/tv_TItle” /》

  《Button android:id=“@+id/sql_addOne” android:layout_width=“fill_parent”

  android:layout_height=“wrap_content” android:text=“插入一条记录”》《/Button》

  《Button android:id=“@+id/sql_check” android:layout_width=“fill_parent”

  android:layout_height=“wrap_content” android:text=“查询数据库”》《/Button》

  《Button android:id=“@+id/sql_edit” android:layout_width=“fill_parent”

  android:layout_height=“wrap_content” android:text=“修改一条记录”》《/Button》

  《Button android:id=“@+id/sql_deleteOne” android:layout_width=“fill_parent”

  android:layout_height=“wrap_content” android:text=“删除一条记录”》《/Button》

  《Button android:id=“@+id/sql_deleteTable” android:layout_width=“fill_parent”

  android:layout_height=“wrap_content” android:text=“删除数据表单”》《/Button》

  《Button android:id=“@+id/sql_newTable” android:layout_width=“fill_parent”

  android:layout_height=“wrap_content” android:text=“新建数据表单”》《/Button》

  《/LinearLayout》

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/dianzi/2630945.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-08-11
下一篇 2022-08-11

发表评论

登录后才能评论

评论列表(0条)

保存