How to connect Dart to SQLite? - sqlite

My old application used web2py with SQLite as the database. Now I want to try porting this app to Dart and again use SQLite as the database.
I can't find any documentation on how to use it. I just found out how to use MySQL with sqljocky.
I've tried to read the web_sql and indexed_db APIs, but I can't find a way to connect to SQLite. How can I use SQLite from Dart?

As far as I can tell, dart-sqlite is the only public attempt at SQLite bindings for Dart, but it's out of date. My guess is that it would actually be pretty easy to update the code and get it up-and-running.
web_sql and indexed_db are both for use in the browser, which, as far as I can tell from your question, isn't what you're looking for.
Update: I updated dart-sqlite so that all the tests now pass, at least on my machine. You're definitely venturing into uncharted territory if you use it, but it's a start.

Related

Scala Slick and SQLite

I'm trying to create a database using Scala and SQLite. I'm using Slick as the library for the SQLite.
I've been googling around for hours and still can't figure out how to get this working. I have eclipse project with Slick installed. I'm trying to instanciate the database with
val db = Database.forUrl("url",driver = "org.SQLite.Driver")
I have no idea what to put in the url. I'm not very sure about the driver part either. Should I use that one or does "scala.slick.driver.SQLiteDriver" work too? Or does it even matter?
I'm really confused about all this.Any help is appreciated
Thanks!
JDBC relies on drivers that implement the JDBC API, and provide access to the low-level functionality of working with particular databases.
URLs are how you tell a JDBC driver which database you want to connect to. The first part of the URL is always jdbc:<driverId>:, where driverId is the specific name that the driver expects to see (e.g. postgresql, mysql or, in your case sqlite.) The format of the URL after the driver ID is specific to the particular driver implementation. With mysql and postgres, where you typically connect over TCP to the database server, you'll see a format like this:
jdbc:mysql://dbserver:dbport/databaseName
jdbc:postgresql://dbserver:dbport/databaseName
But, since SQLite is an in-process, local database, the part of the URL after the driver ID is just a filesystem path, like so:
jdbc:sqlite:/home/me/my-db-file.sqlite

To use SQLite with Datasnap

Is it ok to use SQLite as a backend database in Datasnap ? Will there be a danger of database locking since SQLite is filebased or will the datasnap server provide the necessary backbone for this not to happen ?
I don't know about Datasnap, but with working directly with SQLite (...) I know SQLite is able to use one file/database from several processes and keep track of lock using mutexes. (See more about sqlite's mutex functions here) How they work exactly depends on which compile settings were used when building sqlite3.dll. I've been using the precompiled binaries from the website without any problem.

I can't write to a SQLLite database in my AIR app

I am publishing an AIR app in debug mode using FlashDevelop and have included a database in the files/folders to be published.
When the app first launches it checks whether there is an instance of this db in the applicationStorageDirectory, if there isn't it copies the included one from the applicationDirectory to the applicationStorageDirectory.
This should mean that the referenced database dbFile = File.applicationStorageDirectory.resolvePath(DB_FILE_NAME); should now be writable, however when i run the app i can read the records from the table but when i attempt to write using an SQL statement I get an SQLError: 'Error #3122: Attempt to write a readonly database'.
I know that this would be thrown if i was attempting to write to the read only location of the applicationDirectory but i'm certainly using the File.applicationStorageDirectory location which should (as far as i know) be writable.
The location of the db on my Windows 7, 64bit = C:\Users\sean.duffy\AppData\Roaming\FishFightAppData\Local Store\db this is found using the dbFile.nativePath property so again i'm sure i should be able to update the db.
Anyone got any ideas? I have tried everything i could think of and searched all over but the only common cause seems to be when people try to write to the asplicationDirectory and not the storage directory....
UPDATE::
My bad - have just realised that i've misread the API of the 3rd party library i'm using! I should have been calling executeModify(statement) which can modify the contents of the db, instead i'm calling execute(statement) which doesn't/can't overwrite the db.
The source code is compiled into a swc and there was no documentation to point out you needed to use executeModify, although i should have guessed from the name i suppose!
Sorry about that and thanks for your help
(As a public courtesy to get this off the unanswered list, I am reposting the apparent solution. As usual, the asker is more than welcome to ignore mine and post it themselves and accept their own answer.)
In this API, you need to call executeModify(statement), not execute(statement). The latter does not overwrite the database.

Anyone successfully used password on sqlite database in Monotouch?

I have a Monotouch app which uses a sqlite database. I want to encrypt the database so I am doing this:
_mainConnection = new SqliteConnection("Uri="+finalDB);
_mainConnection.Open();
_mainConnection.ChangePassword("mypassword");
However, its not working (on simulator and iphone). It gets this error:
at (wrapper managed-to-native)
Mono.Data.Sqlite.UnsafeNativeMethods.sqlite3_rekey
(intptr,byte[],int) <0x0005c> at
(wrapper managed-to-native)
Mono.Data.Sqlite.UnsafeNativeMethods.sqlite3_rekey
(intptr,byte[],int) <0x0005c> at
Mono.Data.Sqlite.SQLite3.ChangePassword
(byte[]) <0x00053> at
Mono.Data.Sqlite.SqliteConnection.ChangePassword
(byte[]) <0x0004b> at
Mono.Data.Sqlite.SqliteConnection.ChangePassword
(string) <0x0005b>
Has anyone successfully used password protection on an sqlite database in Monotouch?
As per my research there are a few options for database encryption using MonoTouch. I have a forthcoming blog post on the subject, but for now these are your top two options:
SQLCipher
I've automated the SQLCipher build process substantially. All it takes is a simple make command and you've got a library that you can link into your project. It makes use of the awesome SQLite-NET library. After that, all that's required is to provide the key in the SQLite.cs file.
SQLCipherNet: https://github.com/anujb/SQLCipherNet
CSharp-SQLite
This is a managed port of the SQLite library in C#. Performance is only about ~2x slower, which is pretty awesome considering it's not native code!
Encryption: http://code.google.com/p/csharp-sqlite/wiki/ENCRYPTION
Perf Benchmarks: http://code.google.com/p/csharp-sqlite/wiki/Benchmarks
Try adding ";Password=mypassword" to your connection string, and remove the call to ChangePassword.
Please note that, by default, the iPhone implementation of sqlite does not support encryption, so the sqlite commands for that will be no-ops.
You can get a (paid) copy of the encrypt-able version of sqlite from http://www.hwaci.com/sw/sqlite/see.html, and compile it into your application, making sure to remove the libsqlite3*.dylib from your project if you've linked that in.
You may have to do a bit of digging in the Monotouch documentation and/or experimentation to make sure that the Monotouch library itself is not including the default sqlite implementation, but in fact links to the implementation you specify. Try it first, if things still don't work that's where I'd start looking.
You can do this experiment without paying for the encrypted version, simply using the sqlite3 source code available on the net, with appropriate break points.
Good luck!
PS: Note that there is no comparable solution for Android at this point, this works on iPhone because iPhone runs native C code.
PPS: There is also SQLCipher that claims to encrypt sqlite on iPhone. However I found the configuration requirements to be below my standards for simplicity. I'm also not sure if it will properly insert itself between Monotouch's framework code and the default iPhone sqlite implementation.
SQLCipher for MonoTouch provides full database encryption for SQLite databases.
http://sqlcipher.net/sqlcipher-for-monotouch
There is also a SQLCipher on Mono for Android, which allows you to reuse the same code across Mono Touch and MonoDroid applications
http://sqlcipher.net/sqlcipher-for-monodroid
Just thinking out loud but could this be due to sqlite's dynlib that comes with the iPhoneSDK not being threadsafe?
For an alternative you might try looking at WWDC Vid 209 and just lock/encrypt the DB when you're outside the app.
You can probably do it yourself by issuing a "pragma rekey" in a raw SQLite query -- that is, if the SQLite version installed is actually SqlCipher.
I had the same problem but with a windows form application in C#.
I could not find the solution so i had to encrypt my data manually when saving it and decrypt it when retrieving.

simple question, which part of CoreData hand sql sourced statement?

how can i use SQL statement in CoreData database ? which part of CoreData hand it ? thanks
and how can i fetch only last elements, not all ?
If it were possible to write your own SQL statements and have them executed through the Core Data framework, I think it would be described in the documentation for NSPersistentStore or one of its related classes. As far as I can tell, it isn't.
You can always use SQLite directly to load in (and query) the Core Data database without going through the Core Data framework. This can be done either by using SQLite's own APIs, or the sqlite3 program that should already be installed on your Mac. I'm not sure why you'd want to do this on a database that's already managed by Cora Data, though.

Resources