db_query() miltiple statements
#1

How do I get row-id of a just-inserted row on SQLite?
Reply
#2

i am not quite sure about this but i too needed something like that in past. try this:
SELECT last_insert_rowid();
EDIT: ^ ignore that it didnt worked for me.

i think you will have to play with queries to do so e.g: add new field and set its value to something and reset all others when new query executed etc..
Reply
#3

I mean here's the statement:
Код:
qresult = db_query(sqlite, "SELECT LAST_INSERT_ID()");
qresult holds ZERO this way. What else can I try to get just-inserted row id?
Reply
#4

cache_insert_id after a query (mysql_(p/t)query, doesn't matter), if you want to use it in a INSERT query.

https://sampwiki.blast.hk/wiki/MySQL/R33#cache_insert_id
Reply
#5

Quote:
Originally Posted by IstuntmanI
Посмотреть сообщение
cache_insert_id after a query (mysql_(p/t)query, doesn't matter), if you want to use it in a INSERT query.

https://sampwiki.blast.hk/wiki/MySQL/R33#cache_insert_id
We are talking about SQLite (its implementation into SA:MP) actually... not about MySQL
Reply
#6

Well i think this is not the best idea or maybe very bad idea lol, but this is what i can suggest is:
Add a column named Just_insert to your table. Perform both queries together:
pawn Код:
UPDATE `tableName` SET Just_insert = 0;
INSERT INTO `tableName` (fieldName1,...fieldNameN,Just_insert) VALUES('value1',..'valueN',1)
Note that '1' in end it is to set value of current inserting row to 1 and the above query reset all other valeus to 0.

There can be INSERT query or UPDATE too. Depends since you are asking for latest "Inserted"

laters you can do is:
pawn Код:
SELECT `rowid` from `tableName` WHERE `Just_insert`=1;
it must be handled with care for best results
Reply
#7

Quote:
Originally Posted by Baltazar
Посмотреть сообщение
We are talking about SQLite (its implementation into SA:MP) actually... not about MySQL
My mistake, sorry. Umm... you could also use MySQL, it is pretty much the same thing, and it is better.
Reply
#8

Quote:
Originally Posted by IstuntmanI
Посмотреть сообщение
My mistake, sorry. Umm... you could also use MySQL, it is pretty much the same thing, and it is better.
erm no. it depend on usage which makes it better or no, dotn want any discussion for MySQL vs SQLite . Since SQLite does all good basicly and no bad than MySQL.

Anyway @OP see my post maybe that might be usefull
Reply
#9

There is a feature in sqlitei to do this.

https://sampforum.blast.hk/showthread.php?tid=303682
db_last_insert_rowid(DB:db)

HOWEVER I tried using this once and it was crashing my server I'll ask Slice if he can fix this issue.

You could also try using db_insert() in SQLitei it will return the row id.
pawn Код:
stock db_insert(DB:db, const szQuery[]) {
    new
        DBResult:dbrResult = db_query(db, szQuery, false)
    ;
   
    if (dbrResult) {
        db_free_result(dbrResult);
       
        return db_last_insert_rowid(db);
    }
   
    return 0;
}
Reply
#10

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
There is a feature in sqlitei to do this.

https://sampforum.blast.hk/showthread.php?tid=303682
db_last_insert_rowid(DB:db)

HOWEVER I tried using this once and it was crashing my server I'll ask Slice if he can fix this issue.
didnt knew about that

btw can anyone give thoughts about my method? It SEEM TO BE a very bad method. I just want to know though cuz i was thinking to use like that in past.. but anyway if SQLitei does it good , i will use that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)