driver
pub fn driver() driver.DriverPurpose
Return the database driver instance implementing driver.Driver.
Parameters
No arguments are required.
Returns
driver.Driver — a value of this type
#github.com/xulang-org/packages/sqliteThe SQLite driver uses pure-Xu sqlite_native to dynamically load system libsqlite3 (falls back to built-in winsqlite3.dll on Windows, overridable via XU_SQLITE_LIBRARY), requiring no manual precompilation of native bridges.
github.com/xulang-org/packages · 1.17.2 · Install and use
These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.
36_test_sqlite_pool/main.xu@main
sqlite #github.com/xulang-org/packages/sqlite
#database/sql
#os
#errors
fn remove_if_exists(p string) {
os.remove(p) >>> (e) {
<<
}
}
fn verify_sqlite_pool_deep() {
println("--- SQLite Connection Pool Deep Verification ---")
db_file := "/tmp/xu_sqlite_pool.db"
remove_if_exists(db_file + "")
$db := sqlite.open_pool(db_file + "", 5, 2)
// 1. Ping
db.ping() >>> (err) {
println(" [FAIL] SQLite pool ping failed: " + err.msg())
<<
}
println(" [PASS] SQLite Pool ping succeeded.")
// 2. Schema
db.exec("CREATE TABLE pool_items (id INTEGER PRIMARY KEY, title TEXT, value INT);") >>> (err) {
println(" [FAIL] Create table failed: " + err.msg())
<<
}
println(" [PASS] Schema created.")
// 3. Sequential & Concurrent Inserts through Pool
$i := 1
while i <= 5 {
db.exec("INSERT INTO pool_items (id, title, value) VALUES (?, ?, ?);", i, "item_" + str(i), i * 10) >>> (err) {
println(" [FAIL] Insert failed at item " + str(i) + ": " + err.msg())
<<
}
i = i + 1
}
println(" [PASS] 5 items inserted through SQLite connection pool.")
// 4. Query through Pool
$rows := db.query("SELECT id, title, value FROM pool_items ORDER BY id ASC;") >>> (err) {
println(" [FAIL] Query failed: " + err.msg())
<<
}
$count := 0
while rows.next() {
vals := rows.take() >>> (err) {
<<
}
count = count + 1
}
rows.close()
println(" [PASS] SQLite Pool query verified: retrieved " + str(count) + " rows.")
// 5. Close Pool
db.close() >>> (err) {
println(" [FAIL] Close failed: " + err.msg())
<<
}
os.remove(db_file + "") >>> (e) {
<<
}
println(" [PASS] SQLite Pool closed cleanly.")
}
fn main() {
println("=== SQLite Connection Pool In-Depth Verification ===\n")
verify_sqlite_pool_deep()
println("\n[SQLITE POOL VERIFICATION 100% COMPLETED]")
}22_standalone_sqlite/main.xu@main
#database/sql/driver
sqlite #github.com/xulang-org/packages/sqlite
#os
#strings
fn remove_if_exists(p string) {
os.remove(p) >>> (err) {
<<
}
}
fn main() int {
println("=== SQLite Standalone Library Integration Test ===")
db_file := "/tmp/xu_sqlite_test.db"
remove_if_exists(strings.clone(&db_file))
// 1. 打开数据库连接
conn := sqlite.open(strings.clone(&db_file))
// 2. 建表
schema := "CREATE TABLE IF NOT EXISTS developers (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INT, salary REAL, active INT);"
res1 := conn.exec(schema, []driver.NamedValue{})
if res1.err.len > 0 {
println("CREATE TABLE error: " + res1.err)
<< 1
}
println("1. Schema created successfully.")
// 3. 参数化插入记录
$args1 := []driver.NamedValue{}
args1.push(driver.NamedValue{name: "name", ordinal: 1, value: "Ada Lovelace"})
args1.push(driver.NamedValue{name: "age", ordinal: 2, value: 36})
args1.push(driver.NamedValue{name: "salary", ordinal: 3, value: 125000.5})
args1.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
res_ins1 := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", args1)
if res_ins1.err.len > 0 {
println("INSERT 1 error: " + res_ins1.err)
<< 1
}
println("2. Inserted developer 1, id=" + str(res_ins1.last_insert_id) + ", rows_affected=" + str(res_ins1.rows_affected))
// 4. 第二条记录插入
$args2 := []driver.NamedValue{}
args2.push(driver.NamedValue{name: "name", ordinal: 1, value: "Alan Turing"})
args2.push(driver.NamedValue{name: "age", ordinal: 2, value: 41})
args2.push(driver.NamedValue{name: "salary", ordinal: 3, value: 140000.0})
args2.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
res_ins2 := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", args2)
if res_ins2.err.len > 0 {
println("INSERT 2 error: " + res_ins2.err)
<< 1
}
println("3. Inserted developer 2, id=" + str(res_ins2.last_insert_id) + ", rows_affected=" + str(res_ins2.rows_affected))
// 5. 查询所有记录
rows := conn.query("SELECT id, name, age, salary, active FROM developers ORDER BY id ASC;", []driver.NamedValue{})
if rows.err.len > 0 {
println("QUERY error: " + rows.err)
<< 1
}
println("4. Query returned columns count: " + str(rows.ncols))
$col_str := " Columns: "
for ci in 0:rows.columns.len {
if ci > 0 {
col_str = col_str + ", "
}
col_str = col_str + rows.columns[ci]
}
println(col_str)
nrows := rows.cells.len / rows.ncols
println(" Total rows: " + str(nrows))
$r := 0
while r < nrows {
base := r * rows.ncols
id_str := rows.cells[base]
name_str := rows.cells[base + 1]
age_str := rows.cells[base + 2]
sal_str := rows.cells[base + 3]
act_str := rows.cells[base + 4]
println(" Row " + str(r + 1) + ": id=" + id_str + ", name=" + name_str + ", age=" + age_str + ", salary=" + sal_str + ", active=" + act_str)
r = r + 1
}
// 6. 带参数的查询
$qargs := []driver.NamedValue{}
qargs.push(driver.NamedValue{name: "name", ordinal: 1, value: "Alan Turing"})
rows_filtered := conn.query("SELECT id, name, age FROM developers WHERE name = ?;", qargs)
if rows_filtered.err.len > 0 {
println("FILTERED QUERY error: " + rows_filtered.err)
<< 1
}
if rows_filtered.cells.len >= 3 {
println("5. Parameterized query successful: found " + rows_filtered.cells[1] + ", age=" + rows_filtered.cells[2])
}
// 7. 更新操作
$uargs := []driver.NamedValue{}
uargs.push(driver.NamedValue{name: "age", ordinal: 1, value: 37})
uargs.push(driver.NamedValue{name: "id", ordinal: 2, value: 1})
res_up := conn.exec("UPDATE developers SET age = ? WHERE id = ?;", uargs)
if res_up.err.len > 0 {
println("UPDATE error: " + res_up.err)
<< 1
}
println("6. Updated developer 1: rows_affected=" + str(res_up.rows_affected))
// 8. 事务支持
tx := conn.begin()
$tx_args := []driver.NamedValue{}
tx_args.push(driver.NamedValue{name: "name", ordinal: 1, value: "Grace Hopper"})
tx_args.push(driver.NamedValue{name: "age", ordinal: 2, value: 85})
tx_args.push(driver.NamedValue{name: "salary", ordinal: 3, value: 160000.0})
tx_args.push(driver.NamedValue{name: "active", ordinal: 4, value: true})
res_tx := conn.exec("INSERT INTO developers (name, age, salary, active) VALUES (?, ?, ?, ?);", tx_args)
if res_tx.err.len > 0 {
println("TX INSERT error: " + res_tx.err)
<< 1
}
commit_err := tx.commit()
if commit_err.len > 0 {
println("TX COMMIT error: " + commit_err)
<< 1
}
println("7. Transaction committed successfully.")
// 9. 关闭与清理
conn.close()
remove_if_exists(db_file)
println("=== SQLite Standalone Tests Completed Successfully! ===")
<< 0
}driverpub fn driver() driver.DriverReturn the database driver instance implementing driver.Driver.
No arguments are required.
driver.Driver — a value of this type
openpub fn open(path string) driver.ConnOpen resource or establish connection.
path stringdriver.Conn — a value of this type
struct SqliteDriverpub struct SqliteDriver {
pub tag int
pub path string
}Structure encapsulating tag, path.
tag intpath stringstruct SqliteConnpub struct SqliteConn {
pub st chan SqliteState
}Structure encapsulating st.
st chan SqliteStatestruct SqliteStmtpub struct SqliteStmt {
pub st chan SqliteState
pub q string
}Structure encapsulating st, q.
st chan SqliteStateq stringstruct SqliteTxpub struct SqliteTx {
pub st chan SqliteState
pub id int
pub err string
}Structure encapsulating st, id, err.
st chan SqliteStateid interr stringopen_dbpub fn open_db(path string) sql.DBOpen db.
path stringsql.DB — a value of this type
open_poolpub fn open_pool(path string, max_open int, max_idle int) sql.DBOpen pool.
path stringmax_open intmax_idle intsql.DB — a value of this type