Android平台的SQLite驱动 SQLDroid

码农软件 · 软件分类 · 手机开发包 · 2019-05-07 11:44:28

软件介绍

SQLDroid 是 Android 平台上的 SQLite 数据库的 JDBC 驱动程序。

示例代码:

// your datapath is /data/data/$package/
// e.g. "/data/data/com.lemadi.robotanks.android"

// note that if you want to put it in $datapath/databases/
// the way Activity.openOrCreateDatabase does it,
// you have to create the databases subfolder if not already there

String url = "jdbc:sqldroid:" + getDataPath() + "/main.sqlite";
Connection con = DriverManager.getConnection(url);

con.createStatement().execute("CREATE TABLE MYTABLE (id INT, name CHAR(200))");
                               


try {
        con.createStatement().execute("CREATE TABLE MYTABLE (id INT, name CHAR(200))");
        con.createStatement().execute("CREATE TABLE HIGH_SCORES (level VARCHAR, name CHAR, time INT, timestamp INT)");
} catch (SQLException e1) {
        System.out.println("error creating table: i guess they were already there");
}

con.createStatement().execute("INSERT INTO MYTABLE (id, name) VALUES (100, 'klm')");
               
PreparedStatement ps = con.prepareStatement("INSERT INTO MYTABLE (id, name) VALUES (?, ?)");

ps.setInt(1, (int)(Math.random() * 100));
ps.setString(2, "you're " + new Integer((int)(Math.random()*1000)).toString() + " years old.");
ps.executeUpdate();
               
                       
ResultSet rs = con.createStatement().executeQuery("SELECT id, name FROM MYTABLE ORDER BY name");
                       
while(rs.next()) {
        System.out.println("test row: " + rs.getInt(1) + " = " + rs.getString(2));
        System.out.println("test row string: " + rs.getInt("id") + " = " + rs.getString("name"));
}
                       
rs.close();



// this method demonstrates the limited Metadata functionality:

private static boolean tableExists(String tableName) {
        ResultSet rs = con.getMetaData().getTables(null, null, tableName, null);
        // rs.next() returns true is there is 1 or more rows
        return rs.next();
}

本文地址:https://codercto.com/soft/d/5211.html

暗趋势

暗趋势

王煜全 / 中信出版集团 / 2019-1 / 59元

《暗趋势》由得到“全球创新260讲”专栏主讲人王煜全,为你揭示藏在科技浪潮中的商业机会,教你获得把握趋势的能力,发现小趋势,抓住大机遇。 《暗趋势》聚焦于改变你生活和未来的产业,深度解读人工智能、混合现实、区块链、生物医疗等你必须关注的科技行业,并分析新科技给企业和个人带来的发展机遇,前瞻性提出企业和个人的思维与行动应对策略。 王煜全作为全球科技前哨侦察兵,以其每年5亿元的科技投资及2......一起来看看 《暗趋势》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器