当前位置:首页> 正文

关于动作脚本3:一次使用多个SQLite数据库

关于动作脚本3:一次使用多个SQLite数据库

Using multiple SQLite databases at once

我有2个SQLite数据库,一个是从服务器下载的(server.db),另一个是用作客户端存储的(client.db)。我需要使用服务器数据库中的数据对客户端数据库执行各种同步查询。

例如,我要删除client.db tRole表中的所有行,并重新填充server.db tRole表中的所有行。

另一个示例,我想删除client.db tFile表中不在server.db tFile表中的所有行。

在SQL Server中,您可以在表的前面加上数据库名称。无论如何,在SQLite中使用Adobe Air可以做到这一点吗?


我只是看了AIR SQL API,在SQLConnection上有一个attach方法,它看起来正是您所需要的。

我还没有测试过,但是根据文档,它应该可以工作:

1
2
3
4
5
6
7
8
9
10
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();

该代码段中可能存在错误,最近我对AIR SQL API的使用不多。请注意,使用main.tableName可以打开用open打开的数据库的表,任何附加的数据库都可以被赋予任何名称(在上例中为otherDb)。


可以在Sqlite中一次打开多个数据库,但是在Flex / AIR中工作时是否可以完成却令人怀疑。在命令行客户端中,运行ATTACH DATABASE path/to/other.db AS otherDb,然后可以将该数据库中的表称为otherDb.tableName,就像在MySQL或SQL Server中一样。

Tables in an attached database can be referred to using the syntax database-name.table-name.

ATTACH DATABASE documentation at sqlite.org


这段代码可以用,是我写的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package lib.tools

import flash.utils.ByteArray;
import flash.data.SQLConnection;
import flash.data.SQLStatement;
import flash.data.SQLResult;
import flash.data.SQLMode;
import flash.events.SQLErrorEvent;
import flash.events.SQLEvent;
import flash.filesystem.File;
import mx.core.UIComponent;
import flash.data.SQLConnection;

public class getConn {
    public var Conn:SQLConnection;

    public function getConn(database:Array) {      
        Conn = new SQLConnection();
        var Key:ByteArray = new ByteArray();
        Key.writeUTFBytes("Some16ByteString");
        Conn.addEventListener(SQLErrorEvent.ERROR, createError);
        var dbFile:File = File.applicationDirectory.resolvePath(database[0]);
        Conn.open(dbFile);
        if(database.length > 1) {
            for(var i:Number = 1; i < database.length; i++) {
                var DBname:String = database[i];
                Conn.attach(DBname.split("\\.")[0], File.applicationDirectory.resolvePath(DBname));
            }
        }
        Conn.open(dbFile, SQLMode.CREATE, false, 1024, Key);
    }

    private function createError(event:SQLErrorEvent):void {
        trace("Error code:", event.error.details);
        trace("Details:", event.error.message);
    }

    public function Rs(sql:Array):Object {
        var stmt:SQLStatement = new SQLStatement();
        Conn.begin();
        stmt.sqlConnection = Conn;
        try {
            for(var i:String in sql) {          
                stmt.text = sql[i];
                stmt.execute();
            }
            Conn.commit();
        } catch(error:SQLErrorEvent) {
            createError(error);
            Conn.rollback();
        };
        var result:Object =stmt.getResult();
        return result;
    }
}

展开全文阅读

相关内容