Using jdbc to access sqlite in Android
来源:互联网 发布:天猫双十一数据直播 编辑:程序博客网 时间:2024/06/04 19:44
Why
Android已经提供了SQLiteOpenHelper实现sqlite数据库CRUD等基本操作,但是在某些情况下,可能通过jdbc让java直接访问sqlite数据库更合理。例如:1、复杂逻辑存在的情况下,我们更倾向于自己编写SQL语句并执行;2、希望创建内存数据库并允许频繁、并发访问的情况……
Android自有SQLite内存数据库操作局限性有哪些?TODO
通过jdbc连接sqlite
不同环境下的用法对比
在Windows/Linux环境中,使用Java编程时,可通过sqlite-jdbc 访问并使用sqlite;而在Android环境中,则可通过SQLDroid直接访问sqlite数据库。
两者的区别,仅仅在于加载驱动类名和连接字符串的不同。
数据库SQL基本操作方式相同。
示例
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Sample{ public static void main(String[] args) throws ClassNotFoundException { // load the sqlite-JDBC driver using the current class loader Class.forName("org.sqlite.JDBC"); Connection conn = null; Statement stmt = null; ResultSet rs = null; try { // create a database connection conn = DriverManager.getConnection("jdbc:sqlite:sample.db"); stmt = conn.createStatement(); stmt.setQueryTimeout(30); // set timeout to 30 sec. stmt.executeUpdate("drop table if exists person"); stmt.executeUpdate("create table person (id integer, name string)"); stmt.executeUpdate("insert into person values(1, 'leo')"); stmt.executeUpdate("insert into person values(2, 'yui')"); rs = stmt.executeQuery("select * from person"); while(rs.next()){ // read the result set System.out.println("name = " + rs.getString("name")); System.out.println("id = " + rs.getInt("id")); } } catch(SQLException e){ // if the error message is "out of memory", // it probably means no database file is found System.err.println(e.getMessage()); }finally{ // close rs // close stmt // close conn } }}
哪种方式更快?
对比Android自有SQLite操作类与SQLDroid操作数据库,两种方式在查询上的表现?TODO
reference
SQLDroid Project in github
Xerial sqlite-jdbc
0 0
- Using jdbc to access sqlite in Android
- Using SQLite in Android Application
- Connect to Dababase using JDBC in JSP
- Using SQLite from Shell in Android
- Using CoCreateObjectDotNet to Access and Use .NET Assemblies in InstallScript
- Using Python in Sqlite !
- Using your own SQLite database in Android applications
- Using your own SQLite database in Android applications
- using android toolbox's r to access phys addr
- How to Compile SQLite for Android using NDK
- How to synchronize access to a shared resource in a multithreading environment by using Visual C#
- How to access sqlite database
- 【转载】Using SQLite from Shell in Android(在shell 下使用sqlite命令操作数据库)
- Using the Microsoft Access Providers to Replace the Built-In SQL Server Providers
- How to access database using ADO in VB6 - 在 VB6 中用ADO对象如何访问数据库
- Preventing Access to OMA and Active Sync Using Security Groups in AD
- using an enumerable built-in Ruby to access and manipulate nested data
- Using GPS to get current location in Android
- 开始学习UE4
- 三栏自适应布局实现(左右固定宽度,中间自适应)
- Git学习足迹---常用的基本操作02
- leetcode 2---Add Two Numbers 进位 链表 数学
- OpenGL 图像的加载和存储
- Using jdbc to access sqlite in Android
- C++第6次上机实验
- c++数组的学习
- 内存管理
- sql 获取当前日期的季度,年份,月份等日期部分
- Android ShareSDK 微信,QQ,新浪微博第三方登录
- 最小生成树
- java实体类实现序列化的意义
- MBProgressHUD(透明指示层)详细使用