Android平臺的SQLite驅動 SQLDroid

jopen 12年前發布 | 37K 次閱讀 SQLite 數據庫服務器

SQLDroid 是 Android 平臺上的 SQLite 數據庫 JDBC 驅動程序。可以讓開發人員用熟悉的方式來操作SQLite 數據庫和方便將其它項目,移植到Android平臺中。

示例代碼:

// 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(); }</pre>

項目主頁:http://www.baiduhome.net/lib/view/home/1341990038260

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!