Android游戏开发之SQLite数据库

2022-04-04 00:35:32
导读 大家好,我是本期栏目编辑小友,现在为大家讲解Android游戏开发之SQLite数据库问题。 首先,介绍一些基本概念: 什么是SQLite: SQLite

大家好,我是本期栏目编辑小友,现在为大家讲解Android游戏开发之SQLite数据库问题。

首先,介绍一些基本概念:

什么是SQLite:

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

SQLite的特点:

轻量级选手

使用SQLite时,只需要带一个动态库就可以享受它的所有功能,而且那个动态库的大小应该很小。

独立

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

隔离

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

跨平台

SQLite目前支持大部分操作系统,不仅是电脑操作系统,还有很多手机系统,比如安卓。

多语言界面

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

安全

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

优点:

1.可以存储更多的数据。

2.数据库文件可以存储在SD卡里!

什么是SQLiteDatabase?

SQLiteDatabase的一个实例代表一个SQLiteDatabase。通过SQLite数据库实例的一些方法,我们可以执行SQL语句以及添加、删除、检查和修改数据库。应该注意的是,数据库对于应用程序是私有的,并且数据库的名称在应用程序中是唯一的。

什么是SQLiteOpenHelper?

根据这个名字,我们可以看出这个类是一个辅助类。这个类主要生成一个数据库,管理数据库的版本。当程序中调用这个类的方法getWritableDatabase()或getReadableDatabase()时,如果当时没有数据,那么安卓系统会自动生成一个数据库。SQLiteOpenHelper是一个抽象类。我们通常需要继承它并实现其中的三个功能。

什么是ContentValues类?

ContentValues类类似于Hashmap/Hashtable。它还负责存储一些名称-值对,但是它存储的名称-值对中的名称是一个。

字符串,这些值都是基本类型。

什么是光标?

光标是安卓系统中非常有用的界面。通过游标,我们可以用计算机读写数据库中查询的结果集。

好了,这是基础知识。让我们开始加载代码:仍然按照我一贯的风格,代码中所有应该解释的地方都已经被及时注释和解释了!

顺便拍一张这个项目的截图:

首先给出xml:

查看剪贴板打印的平面图?

10 20 30 40 50 60 70 80 90 100 110 120 mid

dot;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》

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

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

  顺便来张项目截图:

  

  先给出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》

技术专区 pop3和imap的区别介绍 一文看懂POP3、SMTP和IMAP之间的区别和联系 一文看懂802.11ac和802.11n的区别 802.11ac连接的推荐设置_802.11AC无线网桥的配置和注意事项 怎样查看802.11ac协议_查看802.11ac协议的方法(电脑、手机、路由
免责声明:本文由用户上传,如有侵权请联系删除!