Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple SQLite databases at once

I have 2 SQLite databases, one downloaded from a server (server.db), and one used as storage on the client (client.db). I need to perform various sync queries on the client database, using data from the server database.

For example, I want to delete all rows in the client.db tRole table, and repopulate with all rows in the server.db tRole table.

Another example, I want to delete all rows in the client.db tFile table where the fileID is not in the server.db tFile table.

In SQL Server you can just prefix the table with the name of the database. Is there anyway to do this in SQLite using Adobe Air?

like image 450
Shawn Avatar asked Aug 05 '08 16:08

Shawn


People also ask

Can SQLite have multiple databases?

All you need is a disk volume you can create as many SQLite databases as you like. A lesser known feature of SQLite is that you can run queries, including joins, across tables from more than one database. The secret sauce is the ATTACH DATABASE command.

Can SQLite handle concurrency?

Overview. Usually, SQLite allows at most one writer to proceed concurrently. The BEGIN CONCURRENT enhancement allows multiple writers to process write transactions simultanously if the database is in "wal" or "wal2" mode, although the system still serializes COMMIT commands.

Is SQLite multithreaded?

In serialized mode, SQLite can be safely used by multiple threads with no restriction.

Can we use multiple databases for a single application?

Connecting to Multiple Databases For instance, some databases require a default database, whereas others do not. Navicat smooths out these differences by providing a consistent Connection dialog for each database type, with only a few minor variations between screens.


1 Answers

I just looked at the AIR SQL API, and there's an attach method on SQLConnection it looks exactly what you need.

I haven't tested this, but according to the documentation it should work:

var connection : SQLConnection = new SQLConnection();

connection.open(firstDbFile);
connection.attach(secondDbFile, "otherDb");

var statement : SQLStatement = new SQLStatement();

statement.connection = connection;
statement.text = "INSERT INTO main.myTable SELECT * FROM otherDb.myTable";
statement.execute();

There may be errors in that code snippet, I haven't worked much with the AIR SQL API lately. Notice that the tables of the database opened with open are available using main.tableName, any attached database can be given any name at all (otherDb in the example above).

like image 183
Theo Avatar answered Oct 21 '22 00:10

Theo