mozIStorageService 编辑

storage/public/mozIStorageService.idlScriptable This interface lets you open a mozIStorageConnection to a database file, as well as create backups of an unopened database file. This is the only way to open a database connection. 1.0 66 Introduced Gecko 1.8 Inherits from: nsISupports Last changed in Gecko 1.9 (Firefox 3)

See mozIStorageConnection

Method overview

nsIFilebackupDatabaseFile(in nsIFile aDBFile, in AString aBackupFileName, [optional] in nsIFile aBackupParentDirectory);
mozIStorageConnectionopenDatabase(in nsIFile aDatabaseFile);
mozIStorageConnectionopenSpecialDatabase(in string aStorageKey);
mozIStorageConnectionopenUnsharedDatabase(in nsIFile aDatabaseFile);

Methods

backupDatabaseFile()

This method makes a backup of the specified file. The database should not be open, or you should ensure that no database activity is happening when you call this method. The filename provided is only used as a suggestion. If the filename is already taken, attempts will be made to ensure that it is unique. The actual backup file is returned, and the resulting filename can be obtained from that.

nsIFile backupDatabaseFile(
  in nsIFile aDBFile,
  in AString aBackupFileName,
  in nsIFile aBackupParentDirectory Optional
);
Parameters
aDBFile
The database file to back up.
aBackupFileName
The filename to give to the new backup file.
aBackupParentDirectory Optional
The directory to put the backup into. If this is not specified, the backup is placed in the same directory as the original file.
Return value

An nsIFile object representing the newly-created backup file.

openDatabase()

Opens a database connection to the specified file. The specified file is created if it does not already exist. If this method throws NS_ERROR_FILE_CORRUPTED, it's recommended that you call mozIStorageService.backupDatabaseFile() to back up the database so that user data is not lost (although we have no way of recovering this data yet).

Consumers should check mozIStorageConnection.connectionReady to ensure that they can use the database. If this value is false, it is strongly recommended that the database be backed up with mozIStorageConnection.backupDB() so user data is not lost. And then the database can be opened again with the mozIStorageService.openDatabase() method.

Warning: If you have more than one connection to a file, you must use the exact same name for the file each time, including case. The sqlite code uses a simple string comparison to see if there is already a connection. Opening a connection to "Foo.sqlite" and "foo.sqlite" will corrupt your database.

If your database contains virtual tables (for example, for full-text indexes), you must use mozIStorageService.openUnsharedDatabase() to open it, since those tables are not compatible with a shared cache. If you use this method to open a database containing virtual tables, it will think the database is corrupted and throw NS_ERROR_FILE_CORRUPTED.

Note: The connection object returned by this function is not threadsafe. You must use it only from the thread you created it from.
mozIStorageConnection openDatabase(
  in nsIFile aDatabaseFile
);
Parameters
aDatabaseFile
An nsIFile indicating the database file to open.
Return value

A mozIStorageConnection representing the connection to the opened database file.

Exceptions thrown
NS_ERROR_FAILURE
An error occurred attempting to open the database.
NS_ERROR_FILE_CORRUPTED
The database file is corrupted, or contains virtual tables and is not compatible with this method.
NS_ERROR_OUT_OF_MEMORY
Allocating a new storage object failed.

openSpecialDatabase()

Opens a connection to a named special database storage that identifies the type of storage requested.

Note: You should only access the profile database from the main thread since other callers may also be using it.
mozIStorageConnection openSpecialDatabase(
  in string aStorageKey
);
Parameters
aStorageKey
A string key identifying the type of storage requested. Valid values are "profile" and "memory".
Return value

A new mozIStorageConnection providing access to the requested database.

Exceptions thrown
NS_ERROR_INVALID_ARG
If aStorageKey is invalid.

openUnsharedDatabase()

Opens a database connection to the specified file without using a shared cache. If this method throws NS_ERROR_FILE_CORRUPTED, it's recommended that you call mozIStorageService.backupDatabaseFile() to back up the database so that user data is not lost (although we have no way of recovering this data yet).

Each connection uses its own sqlite cache, which is inefficient, so you should use openDatabase() instead of this method unless you need a feature of SQLite that is incompatible with a shared cache, like virtual table and full text indexing support. If cache contention is expected, for instance when operating on a database from multiple threads, using unshared connections may be a performance win.

Warning: If you have more than one connection to a file, you must use the exact same name for the file each time, including case. The sqlite code uses a simple string comparison to see if there is already a connection. Opening a connection to "Foo.sqlite" and "foo.sqlite" will corrupt your database.

Note: The connection object returned by this function is not threadsafe. You must use it only from the thread you created it from.
mozIStorageConnection openUnsharedDatabase(
  in nsIFile aDatabaseFile
);
Parameters
aDatabaseFile
An nsIFile indicating the database file to open.
Return value

A mozIStorageConnection representing the connection to the opened database file.

Exceptions thrown
NS_ERROR_FAILURE
An error occurred attempting to open the database.
NS_ERROR_FILE_CORRUPTED
The database file is corrupted, or contains virtual tables and is not compatible with this method.
NS_ERROR_OUT_OF_MEMORY
Allocating a new storage object failed.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:123 次

字数:11373

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文