B4J Question [BANanoSQL] How to get a record by 'key'

Mashiane

Expert
Licensed User
Longtime User
Ola

1607746944481.png


With auto_increment turned on when creating a table, one is able to add records with an auto-generated key, perfect!

Q1: How do I get that record by reading the key when the value does not include it?
Q2: How do I run an SQL select that will include the key as part of the returned objects?

Thanks
 
Last edited:

alwaysbusy

Expert
Licensed User
Longtime User
It would help if we see your CREATE TABLE query.

BANanoSQL has a LastInserted() method. What does that return (right after your INSERT)?
What does SELECT * FROM return? It could give you a clue where the key is stored in AlaSQL.

I personally use UUIDs as key (not auto incremented of course). This allows me to be prepared if I want to sync the browser records to my server DB.

Alwaysbusy
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
The table create command is:

B4X:
CREATE TABLE IF NOT EXISTS [users] ([id] INT PRIMARY KEY AUTOINCREMENT, [age] INT, [name] TEXT)

The insert command is like...

B4X:
INSERT INTO [users] ([name], [age]) VALUES (?, ?) and the args are for example,  ["Faulkner", 35]

The inserts will add records like... of course these does not have the auto-increment id value. This assumption was based on how other dbs work.

1607863123061.png


Update:

As BANanoSQL is a wrap of AlaSQL, I bumped into some function call on the net to ensure that each record gets the auto-increment id. A function named autoval.

Its being discussed in this topic, https://dev.to/jorge_rockr/alasql-a-real-database-for-web-browsers-and-nodejs-24gj

I'm still checking if possible to implement something similar, perhaps using RunMethod etc.

Thanks
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
BANanoSQL has a LastInserted() method. What does that return (right after your INSERT)?

Running in debug mode...

B4X:
// [105]  Dim x As String = db.LastInserted( {68} , {69} )
_x=alasql.autoval("users", "id");
// [106]  Log(x)
console.log(_x);

Unfortunately it returns an error.

1607868327598.png


Its nice to see that this was already built in, sadly its just not working at the moment. Otherwise this is the perfect solution for what I need.

Regards

TheMash
 
Last edited:
Upvote 0
Top