sqlite_3 Last Insert rowid - ios

I am working with an SQLite database on iOS, but I cannot figure out how to get the last inserted row ID. The sqlite3_last_insert_rowid() function only works with the current connection, so it is useless if i have just opened the database.
In C#, I am used to calling ExecuteScalar() with the SQL query SELECT last_insert_rowid(). Is there a way to do this using sqlite3?

No, sorry, you can only use it on the same db connection:
http://www.sqlite.org/c3ref/last_insert_rowid.html

Related

Delphi FireDAC: how to refresh data in cache

i need to refresh data in a TFDQuery which is in cached updates.
to simplify my problem, let's suppose my MsACCESS database is composed of 2 tables that i have to join.
LABTEST(id_test, dat_test, id_client, sample_typ)
SAMPLEType(id, SampleName)
in the Delphi application, i am using TFDConnection and 1 TFDQuery (in cached updates) in which i join the 2 tables which script is:
"SELECT T.id_test, T.dat_test, T.id_client, T.sample_typ, S.SampleName
FROM LABTEST T
left JOIN SAMPLEType S ON T.sample_typ = S.id"
in my application, i also use a DBGrid to show the result of the query.
and a button to edit the field "sample_typ", like this:
qr.Edit;
qr.FieldByName('sample_typ').AsString:=ce2.text;
qr.Post;
the edition of the 'sample_typ' field works fine but the corresponding 'sampleName' field is not changing (in the grid) after an update.
in fact it is not refreshed !
the problem is here: if i do refresh of the query, an exception is raised: "cannot refresh dataset. cached updates must be commited or canceled
and batch mode terminated before refreshing"
if i commit the updates, data will be sent to database and i don't want that, i need to keep the data in cache till the end of the operation.
also if i get out of the cache, data will be refreshed in the grid but will be sent to the database after qr.post and i don't want that.
i need to refresh data in the cache. what is the solution ?
Thanks in advance.
The issue comes down to the fact that you haven't told your UI that there is any dependency on the two fields - it clearly can't know how to do the join itself without resubmitting it so if you don't want to send the updates and reload you will have a problem.
It's not clear exactly what you are trying to do, but these two ideas may help you.
If you are not going to edit the fields in the SAMPLEType tables (S) then load the values from that table into a lookup table. You can load this into a TFDMemTable. You can use an adapter which loads from a query. Your UI controls can then show the value based on the valus looked up in your local TFDMemTable. Dependiong on the UI control this might be a 'LookupField' or some such.
You may also be able to store your main data in a TFDMemTable with an Adapter - you can specify diferent TFDCommands to read the whole recordset, refresh a record, update, insert and delete a record. The TFDCommands can act on multiple tables for joined recordsets like this. That would automatically refresh the individual record for you when you post it.

AnyDac aka FireDac cannot generate update query

I was using UniDac for a long time now and decided to move on to FireDac as it has good Asynch methods after moving on i saw that none of my data editing works anymore it gives me an error:
[FireDAC][Phys]-330. Cannot generate update query. Update table undefined.
What I am trying to do here is i have a TFDStoredProc component who gets all the data from the database and lets me edit it, with unidac I could easily edit the data without any problem like this:
StoredProc.Edit;
StoredProcCreatedID.Value := SomeValue;
StoredProc.Post;
and it worked, but with AnyDac it doesn't, I tried specifying manually the UpdateTable which leads to another problem:
[FireDAC][Phys][ODBC][Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid column name 'CreatedID'.
I am using Microsoft SQL Server 2012 FireDac 8.0 and stored procedures for getting back results any ideas?
P.S.
The query looks like this
SELECT
CreatedBy as CreatedID,
usr.UserName as CreatedBy
FROM
Sales
LEFT JOIN
Users usr ON usr.ID = Sales.CreatedBy
it looks like the FireDac update builder doesn't recognize the aliases on the fields, any help would be appreciated.
Well i figured out what was the issue, it seems that if you specify a alias in the query for a field the Origin property will be set to the alias and not the real field i downloaded CNPack a must have for a delphi developer also its free, ran the component selector and changed all my aliased fields to their real fields and it works, but this is still a big issue in FireDac component because it doesnt recognize the aliased fields, lets hope it will be fixed in the future as to specify for every query what table it should update and what fields that just allot of work if you are migrating from a big project in my case 220+ stored procedures.

Fetching only changed SQL result from server

My iOS application fetches some photos, tags and comments from web server. I want it to fetch only changed or new added data. I don't want it to fetch repeated data again and again.
I use SDWebImage for pictures. But text are based on SQL text.
How could I understand the result of the SQL is changed or not? What kind of technique
should I use?
Is there a third party library for client side SQL catching?
I think it is not iOS related question technically. You should query always with the last queried timestamp.
Like:
SELECT * FROM comments WHERE last_modified > last_queried_timestamp;
last_modified field should store the timestamp of the last modification date or the timestamp of creation and the last_queried_timestamp parameter is the timestamp of the last date when you queried from the server.
This way you will not get twice the same changes.
(Unless you want it)

Uknown error finalizing or resetting statement in sqlite

I'm trying to get our current database to use FTS. I recompiled the project with FTS3 and FTS4 support on. I know this part works since I've tried some of the examples off of the sqlite pages and they work.
e.g.
CREATE TABLE t2(id INTEGER PRIMARY KEY, a, b, c);
CREATE VIRTUAL TABLE t3 USING fts4(content="t2", a, c);
What I want to do is add a Virtual table to a .db that is already saved to the documents directory of our app. It is writeable.
My question is multi part. First, I tried my query in Firefox's add-on SQLite manager. My query is this:
CREATE VIRTUAL TABLE VirtualTestTable USING fts4(content=Chromosomes, ID Integer Primary Key Autoincrement, rsID, ChrID, Position, Strand, GeneSymbol)
Once I run this in sqlite manager, it creates a few different tables (VirtualTestTable, VirtualTestTable_docsize, VirtualTestTable_segdir, VirtualTestTable_segments, VirtualTestTable_stat).
So I assume sqlite manager supports FTS4. My table is also populated with the data that was original in my Chromosomes Table. But when I try to match anything in the table, I always get zero results. So my first question is, does sqlite manager support FTS queries?
My other question, which is more important to me, is if my query works in sqlite manager, I try this query in my Xcode project with different variations of
content='Chromosomes'
content=\"Chromosomes\"
content=Chromosomes
I always get an error with my query. I'm using FMDB and using the executeUpdate: message. My error is:
Error calling sqlite3_step (1: SQL logic error or missing database) SQLITE_ERROR
Unknown error finalizing or resetting statement (1: unrecognized parameter: content=Chromosomes)
Does anyone know why I'm getting this error? I don't think it's a missing database as when I try to just create an empty Virtual table on my fmdatabase, it runs fine. Thanks.
FTS queries work just fine in SQL Manager:
CREATE VIRTUAL TABLE t USING FTS4(x);
INSERT INTO t VALUES('blah');
INSERT INTO t VALUES('blubb');
SELECT * FROM t WHERE x MATCH 'bl*';
The error message "unrecognized parameter: content=" is typical for SQLite versions that do not support contentless/external content tables, i.e., ones older than 3.7.11.

Entity Framework Update statement generating invalid SQL for Firebird

I'm using Firebird 2.5.0 with the Entity Framework, which has been working well. However, recently I have changed something (I presume) and now the SQL that is being generated when I try to save my changes is not valid for Firebird:
update ( select [fields] from [table]) set [field] = #p0 where ([keyfieldn = #pn])
As far as I know, Firebird 2.5.0 does not support this syntax, and when I try to save changes I get back a SQL error that says as much ( "Token unknown, column 8: (" )
I'm not sure what to do now. What would cause the UpdateTranslator instance to generate this SQL instead of whatever it must have been generating before I broke it?
I went back through the edmx file and found that I had gotten some 'DefiningQuery' elements when I had it pull in some new fields from the database. I couldn't find any way to access those settings in the interface, so I just deleted them from the file and it is working correctly again.

Resources