codingdir logo sitemap sitemap |

Extract/Query/Read SQLite database row by ID

By : , Category : android

For your selection argument, you may need to surround the target value with single quotes like so:

KEY_ROWID + "='" + id + "'"

Alternatively, you could use the selectionArgs parameter as well like so:

String selection = KEY_ROWID + "=?";
String[] selectionArgs = {id};
Cursor mCursor = mDb.query(true, SQLITE_TABLE, new String[] {
                selection, seelctionArgs, null, null, null, null);
ReLated :

First of all, for a Task you only have to yield things that return promises and therefore run asynchronously. There is no need to yield iDs.push(row[i].getResultByIndex(0)); because the push operation will synchronously return the new length of the array. This shouldn't be much of an issue for the code itself though.

Do you really need the ids to be global? Maybe you can refactor your code so that you don't need to save them globally.

If this is not an option, you will have to block all operations that are going to access the ids until the sql call has completed. You can do this by relying on the fact that Task.spawn() itself will also return a promise. This also has the nice side effect that you don't need the extra global array:

let idsPromise = Task.spawn(function*() {
    let ids = [];
    let db = yield Sqlite.openConnection({ path: permissionFilePath});

    try {
        let row = yield db.execute("SELECT id FROM 'moz_hosts'");
        for (let i = 0, len = row.length; i < len; i++) {
        // Instead of the loop you can also use:
        // ids = => row.getResultByIndex(0));
    } finally {
        yield db.close();

    return ids;  

Then, in other parts of your code, when you want the ids you can use:

idsPromise.then(function(ids) {
    // Do something with ids

Alternatively, you can also get them in a Task:

Task.spawn(function*() {
    let ids = yield idsPromise;

You can do this multiple times, once a promise is resolved the then() part will be executed as soon as possible.

SQLite is an embedded database; it is supposed to be integrated into some program that actually does something with the data.

If you want to access the database directly in Navicat, you could create a view for each of those queries. This still requires you to execute something like

SELECT * FROM NaughtyOrNiceReport

in Navicat, but it's the best you can do.

If you want to use 24-hour time you need to use HH in your format string. hh is for 12-hour time and the supplied time is > 12, so an exception is thrown. This will work.

DateTime dt = DateTime.ParseExact(out_date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

SQLite only has a small set of date and time functions. You can use them like this:

sqlite> .dump
PRAGMA foreign_keys=OFF;
CREATE TABLE t1 (f1 string);
INSERT INTO "t1" VALUES('03/31/1970');

sqlite> select substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2) from t1;

sqlite> select strftime("%m", substr(f1, 7) || '-' || substr(f1, 0, 3) || '-' || substr(f1, 4, 2)) from t1;

Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © All Rights Reserved .