Last active
January 19, 2016 22:00
-
-
Save sebastianrothbucher/e1d1d2d85658832fc7f2 to your computer and use it in GitHub Desktop.
Little SQLite parallel test (2x2 in parallel) - should produce 4k recs (adopted from https://github.com/mapbox/node-sqlite3)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sqlite3 = require('sqlite3').verbose(); | |
var db = new sqlite3.Database('mydb.sqlite'); | |
db.run("CREATE TABLE if not exists lorem (num NUMBER, info TEXT)"); | |
var stmt = db.prepare("INSERT INTO lorem VALUES (?, ?)"); | |
var stmtIncr = db.prepare("update lorem set num=num+1000 where num<1000"); | |
var i = 0; | |
function nextRun() { | |
if(i < 1000) { | |
console.log(" > " + i); | |
stmt.run(i, "Ipsum " + i); | |
if (((i + 1) % 100) == 0) { | |
console.log(" >> upd"); | |
stmtIncr.run(); | |
} | |
i++; | |
setTimeout(nextRun, 50); | |
} else { | |
stmt.finalize(); | |
stmtIncr.finalize(); | |
printRes(); | |
} | |
}; | |
function printRes() { | |
db.each("SELECT rowid AS id, num, info FROM lorem order by num", function(err, row) { | |
console.log(row.id + ": " + row.info + " (" + row.num + ")"); | |
}); | |
db.close(); | |
}; | |
nextRun(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
public class SQLiteJDBC { | |
public static void main(String args[]) throws SQLException, ClassNotFoundException { | |
Connection c = null; | |
Class.forName("org.sqlite.JDBC"); | |
c = DriverManager.getConnection("jdbc:sqlite:mydb.sqlite"); | |
c.createStatement().executeUpdate("CREATE TABLE if not exists lorem (num NUMBER, info TEXT)"); | |
PreparedStatement stmt = c.prepareStatement("INSERT INTO lorem VALUES (?, ?)"); | |
PreparedStatement stmtIncr = c.prepareStatement("update lorem set num=num+1000 where num<1000"); | |
for (int i = 0; i < 1000; i++) { | |
System.out.println(" > " + i); | |
stmt.setInt(1, i); | |
stmt.setString(2, "Ipsum " + i); | |
stmt.executeUpdate(); | |
if (((i + 1) % 100) == 0) { | |
System.out.println(" >> upd"); | |
stmtIncr.executeUpdate(); | |
} | |
try { | |
Thread.sleep(50); | |
} catch (InterruptedException e) { | |
// ignore spurious wakeups | |
} | |
} | |
ResultSet res = c.createStatement().executeQuery("SELECT rowid AS id, num, info FROM lorem order by num"); | |
while (res.next()) { | |
System.out.println(res.getString("id") + ": " + res.getString("info") + " (" + res.getInt("num") + ")"); | |
} | |
c.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment