What you learn:You will learn how tocreatedatabases andtables,insertandquerydatasets in the Android-built-inSQLite-DataBase-Server.
Difficulty:1 of 5
Questions/Problems:Simply post below...
What it will look like:
Description:We'll need to to the following things:
- Create a DataBase(generally this is done just once)
- Open the DataBase
- Create a Table(generally this is done just once)
- Insert some Datasets
- Query for some Datasets
- Close the Database
0.)So lets work it out:
We first do some setup. Declaring the DataBases/Tables we are using as final should always be preferred before typing the name to every single statement. (Changes are a lot easier !).
Java: |
publicclassDataBaseWorkextendsListActivity{
privatefinalStringMY_DATABASE_NAME ="myCoolUserDB"; privatefinalStringMY_DATABASE_TABLE ="t_Users";
/** Called when the activity is first created. */ @Override publicvoidonCreate(Bundle icicle){ super.onCreate(icicle); /* Will hold the 'Output' we want to display at the end. */ ArrayListresults =newArrayList();
|
1.)So lets create the DataBase:
Java: |
SQLiteDatabase myDB =null; try{ /* Create the Database (no Errors if it already exists) */ this.createDatabase(MY_DATABASE_NAME,1, MODE_PRIVATE,null);
|
2.)Having created the DataBase we want to open it:
Java: |
/* Open the DB and remember it */ myDB =this.openDatabase(MY_DATABASE_NAME,null);
|
3.)Now we create a simple Table with just four columns:
Java: |
/* Create a Table in the Database. */ myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DATABASE_TABLE +" (LastName VARCHAR, FirstName VARCHAR," +" Country VARCHAR, Age INT(3));");
|
4.)Put two DataSets to the recently created Table:
Java: |
/* Add two DataSets to the Table. */ myDB.execSQL("INSERT INTO " + MY_DATABASE_TABLE +" (LastName, FirstName, Country, Age)" +" VALUES ('Gramlich', 'Nicolas', 'Germany', 20);"); myDB.execSQL("INSERT INTO " + MY_DATABASE_TABLE +" (LastName, FirstName, Country, Age)" +" VALUES ('Doe', 'John', 'US', 34);");
|
5.)Having written some DataSets to the Table, we would want to receive them back somewhen. Thr result of a query is a Cursor that can move over all the results returned by the query. We apply Projection (Just the Specified Columns) and Selection (WHERE ...) to it and a LIMIT. Just as we would do in any other SQL-"Dialect":
Java: |
/* Query for some results with Selection and Projection. */ Cursorc = myDB.query("SELECT FirstName,Age"+ " FROM "+ MY_DATABASE_TABLE +" WHERE Age > 10 LIMIT 7;", null);
|
6.)Now having queried, we retrieve the ColumIndexes of two Columns calling thegetColumnIndex(String);-method of the Cursor:
Java: |
/* Get the indices of the Columns we will need */intfirstNameColumn = c. getColumnIndex("FirstName"); intageColumn = c. getColumnIndex("Age"); /* Check if our result was valid. */if(c != null){/* Check if at least one Result was returned. */if(c. first()){inti = 0; /* Loop through all Results */do{i++; /* Retrieve the values of the Entry * the Cursor is pointing to. */StringfirstName = c. getString(firstNameColumn ); intage = c. getInt(ageColumn ); /* We can also receive the Name * of a Column by its Index. * Makes no sense, as we already * know the Name, but just to shwo we can*/StringageColumName = c. getColumnName(ageColumn ); /* Add current Entry to results. */results. add(""+ i + ": "+ firstName + " ("+ ageColumName + ": "+ age + ")"); }while(c. next()); }}
|
7.)Finally close the DataBase (if it has been opened):
Java: |
}catch(FileNotFoundExceptione){ }finally{ if(myDB !=null) myDB.close(); }
|
8.)In the end, display our Entries:
Java: |
this.setListAdapter(newArrayAdapter(this, android.R.layout.simple_list_item_1_small, results)); } }
|
分享到:
相关推荐
赠送jar包:sqlite-jdbc-3.34.0.jar; 赠送原API文档:sqlite-jdbc-3.34.0-javadoc.jar; 赠送源代码:sqlite-jdbc-3.34.0-sources.jar; 赠送Maven依赖信息文件:sqlite-jdbc-3.34.0.pom; 包含翻译后的API文档:...
赠送jar包:sqlite-jdbc-3.15.1.jar; 赠送原API文档:sqlite-jdbc-3.15.1-javadoc.jar; 赠送源代码:sqlite-jdbc-3.15.1-sources.jar; 赠送Maven依赖信息文件:sqlite-jdbc-3.15.1.pom; 包含翻译后的API文档:...
android-database-sqlcipher-4.4.0和sqlite-2.1.0 官网下载太慢了,还总是下不下来,终于下载下来就分享出来。 安装清参考博客:https://blog.csdn.net/zdwcmy/article/details/106990422
"sqlite-jdbc-3.32.3.2.jar"是这个驱动程序的特定版本,它包含了用于连接SQLite数据库的必要组件。 这个版本的sqlite-jdbc驱动特别强调对信创(创新信息技术)环境的支持,这通常指的是中国自主研发的CPU架构和操作...
sqlite-jdbc-3.36.0.3.jar 最新吧2021 8月底更新
sqlite-tools-win32-x86-3290000 是一个SQLite数据库工具在Windows 32位系统上的安装包或目录名称。SQLite是一个C库,提供了一个轻量级的磁盘文件数据库,不需要一个单独的服务器进程或操作系统(不需要配置、安装或...
sqlite-devel-3.7.17-8.el7.x86_64.rpm
在本例中,我们关注的是GeoServer的一个特定插件——"gwc-sqlite-plugin",这是针对GeoServer的全球瓦片缓存(Global Web Coverage Service, GWC)功能的SQLite扩展。 GeoServer的GWC功能允许高效地存储和分发地理...
在本案例中,“sqlite-jdbc-3.31.1.zip”是一个包含SQLite JDBC驱动的压缩包,版本号为3.31.1,用于在Java环境中通过JDBC方式连接和操作加密后的SQLite数据库。 首先,我们需要了解如何在Java项目中使用sqlite-jdbc...
本主题主要围绕“Sqlite-jdbc-3.7.2.jar”和“sqlite-jdbc-3.20.1.jar”两个Java JDBC驱动进行讨论,它们是Java连接SQLite数据库的关键组件。 Sqlite-jdbc驱动是Java开发者用来与SQLite数据库进行交互的桥梁。JDBC...
"sqlite-tools-win-x64-3440200.zip"这个压缩包包含了三个主要的SQLite实用工具,它们分别是sqlite3_analyzer.exe、sqlite3.exe和sqldiff.exe。 1. **sqlite3.exe**:这是SQLite的命令行接口,也是最基础的工具。...
"sqlite-autoconf-3080800.tar.gz" 是一个包含SQLite源代码的压缩包,版本号为3080800,通过自动配置脚本编译。这个文件通常用于开发环境,允许开发者在自己的系统上编译和定制SQLite库。 SQLite的特性包括: 1. *...
SQLite数据库是文档型数据库,其具备体积小移动方便等特点;以下jar包:sqlite-jdbc-3.30.1.jar文件为SQLite数据库对应的数据库驱动jar包;
sqlite-jdbc-3.34.0.jar,支持M1芯片了,Android Studio可以正常链接手机了
"sqlite-autoconf-3330000.tar.gz"是SQLite3的一个特定版本(3330000)的源代码包,通常用于开发人员下载和编译以适应其项目需求或进行自定义扩展。 SQLite3的核心特性包括: 1. **无服务器模式**:与大多数数据库...
在本场景中,我们关注的是如何使用 `sqlite-jdbc-3.8.11.1.jar` 这个特定版本的驱动来实现 Java 与 SQLite 的交互。 首先,要使用 SQLite JDBC,你需要将 `sqlite-jdbc-3.8.11.1.jar` 添加到你的项目类路径中。如果...
`sqlite-autoconf-3290000.tar.gz` 是SQLite的一个源码包,版本号为3290000,通过这个包我们可以获取SQLite的原始代码并进行自定义编译和安装。 首先,让我们详细了解一下源码编译的基本步骤: 1. **解压缩**:...
2. 在WordPress的wp-content/plugins目录下创建一个新的文件夹,命名为“sqlite-integration”。 3. 将解压缩后得到的sqlite-integration文件夹中的所有内容移动到刚创建的目录中。 4. 登录到你的WordPress管理后台...
sqlite-autoconf-3320300 嵌入式数据库源码.编译命令:./configure --host=arm-linux(这里根据需要可以改为gcc,即省去该配置参数(删除--host),默认使用gcc) --prefix=/tmp/build
标题“sqlite-autoconf-3071000.tar.gz”指的是SQLite3的一个特定版本的源代码包,其中“sqlite-autoconf-3071000”是该版本的标识符,而“.tar.gz”表示这是一个使用gzip压缩的tar归档文件。这种格式在Linux和Unix-...