Firefox SQLite extension saving and getting

Good now for juicy things. All attempts failed to save my string.

Here is the code to save it in sqllite in the Firefox extension:

var file = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties) .get("ProfD", Components.interfaces.nsIFile); file.append("my_db_file_name.sqlite"); var storageService = Components.classes["@mozilla.org/storage/service;1"] .getService(Components.interfaces.mozIStorageService); var mDBConn = storageService.openDatabase(file); mDBConn.execute("CREATE TABLE IF NOT EXISTS log_det (id INTEGER PRIMARY KEY AUTOINCREMENT, acc STRING)"); mDBConn.execute("INSERT INTO log_det (acc) VALUES(" + window['gluistr']+ ")"); mDBConn.drop(); 

And the code to extract the value:

 var file = Components.classes["@mozilla.org/file/directory_service;1"] .getService(Components.interfaces.nsIProperties) .get("ProfD", Components.interfaces.nsIFile); file.append("my_db_file_name.sqlite"); var storageService = Components.classes["@mozilla.org/storage/service;1"] .getService(Components.interfaces.mozIStorageService); var mDBConn = storageService.openDatabase(file); var res = mDBConn.execute("SELECT * FROM log_det"); mDBConn.drop(); 

Does not work. Does anyone know why? Execute ok or I need createStatement or executeSimpleSQL. I am embarrassed.

+4
source share
1 answer

Use executeSimpleSQL .

openDatabase returns a mozIStorageConnection instance that has no method named execute . You can use executeSimpleSQL anytime you want to execute an SQL statement without related parameters (this is what you do).

You were probably thinking of the mozIStorageStatement execute method . executeSimpleSQL not enough if related parameters are needed. Instead, you need to create an operator, bind any parameters and then execute it:

 var statement = mDBConn.createStatement( "SELECT * FROM log_det WHERE column_name = :parameter"); statement.bindStringParameter(0, "value"); statement.execute(); statement.reset(); 

Also note that mozIStorageConnection does not have a method called drop . Maybe you wanted to write mDBConn.close() ?

All of this is described here:

+3
source

Source: https://habr.com/ru/post/1342990/


All Articles