Jump to content

How to get or read a single data in SQLite database?


Recommended Posts

Only the ISO timestamp format can sort natively. mm/dd/yyyy and other variations are for human consumption only; forget it in storage!

You can still display the format of your choice in the resultset of a query: use string functions for that.

Also, if you want local time, don't use a literal fixed value, use the 'localtime' modifier in the date functions. That way summer/winter times are correct. I recommend storing UTC times and display local times when needed. If you store local times, you never know if that was east- or west-coast time or anything in between (if you live in the US for instance).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Looking again at your last post, I'd urge you to normalize your database. Category should be a separate table and you should use it as a foreign key in your main table.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

On 9/4/2024 at 4:53 PM, jchd said:

Looking again at your last post, I'd urge you to normalize your database. Category should be a separate table and you should use it as a foreign key in your main table.

could you explain about separate table and use it as foreign key?

I also have another question
If several column value in a row has been inserted, how can I insert the rest that is still null?
I mean how to insert / update only description part in the picture below?

Oh yes, Is there any udf or function to use mysql or another sql?
Could you please tell me?

Thank You
 

Newest Book.png

Link to comment
Share on other sites

Here is an example about what @jchd told you about having a foreign key on Category and an update example.

#include <SQLite.au3>
#include <Array.au3>

_SQLite_Startup()
$hDB = _SQLite_Open()

; Create categories and products tables
_SQLite_Exec($hDB, 'CREATE TABLE categories(' & _
                                'idCategory INTEGER PRIMARY KEY,' & _
                                'Category VARCHAR(32) NOT NULL' & _
                            ');' _
            )

_SQLite_Exec($hDB, 'CREATE TABLE products' & _
                            '(idProduct INTEGER PRIMARY KEY,' & _
                            'idCategory INTEGER NOT NULL,' & _
                            'ProductName VARCHAR(64) NOT NULL,' & _
                            'Price DECIMAL(10, 5) NOT NULL,' & _
                            'RegisterDate DATETIME NOT NULL,' & _
                            'Description TEXT,' & _
                            'FOREIGN KEY(idCategory) REFERENCES categories(idCategory)' & _
                        ');' _
            )

; Insert categories
_SQLite_Exec($hDB, 'INSERT INTO categories(Category) VALUES("Book"), ("Pen"), ("Eraser");')

; Insert some products
_SQLite_Exec($hDB, 'INSERT INTO products(idCategory, ProductName, Price, RegisterDate) VALUES' & _
                   '(1, "Book A", 55000, "2024/09/05 10:10:10"),' & _
                   '(1, "Book B", 35000, "2024/09/01 15:10:10"),' & _
                   '(1, "Book C", 75000, "2024/09/02 11:10:10"),' & _
                   '(1, "Book D", 15000, "2024/09/04 10:10:10"),' & _
                   '(2, "Pen A", 15000, "2024/09/03 15:10:10"),' & _
                   '(2, "Pen B", 25000, "2024/09/03 13:10:10"),' & _
                   '(2, "Pen C", 35000, "2024/09/04 08:10:10"),' & _
                   '(2, "Pen D", 20000, "2024/09/04 09:10:10"),' & _
                   '(3, "Eraser A", 25000, "2024/08/04 23:10:10"),' & _
                   '(3, "Eraser B", 25000, "2024/10/04 13:10:10"),' & _
                   '(3, "Eraser C", 25000, "2022/08/04 15:10:10"),' & _
                   '(3, "Eraser D", 25000, "2025/03/04 23:50:10")' _
            )

Local $aResult, $iRows, $iColumns

; Read all products from table
_SQLite_GetTable2d($hDB, 'SELECT p.idProduct, c.Category, p.ProductName, p.Price, p.RegisterDate, p.Description ' & _
                         'FROM products p INNER JOIN categories c ON p.idCategory=c.idCategory;', $aResult, $iRows, $iColumns)
_ArrayDisplay($aResult)

; Read all products from table ordered by date descending
_SQLite_GetTable2d($hDB, 'SELECT p.idProduct, c.Category, p.ProductName, p.Price, p.RegisterDate, p.Description ' & _
                         'FROM products p INNER JOIN categories c ON p.idCategory=c.idCategory ' & _
                         'ORDER BY RegisterDate DESC;', $aResult, $iRows, $iColumns)
_ArrayDisplay($aResult)

; Update description of the first product
_SQLite_Exec($hDB, 'UPDATE products SET Description = "Whatever you want" WHERE idProduct = 1;')
_SQLite_GetTable2d($hDB, 'SELECT p.idProduct, c.Category, p.ProductName, p.Price, p.RegisterDate, p.Description ' & _
                         'FROM products p INNER JOIN categories c ON p.idCategory=c.idCategory;', $aResult, $iRows, $iColumns)
_ArrayDisplay($aResult)

_SQLite_Close($hDB)
_SQLite_Shutdown()

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

@Andreik@jchd
How can I insert description into my table below?

I can use this code below to do that:

update Book set Description = 'This book has newer release' where Product = 'Book A'

But, can I use another command, something like:

insert Into Book (Description) Values ('This book has newer release') where Product = 'Book A'?

Or another command maybe?

Thank You

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Oh yes, Is there any udf or function to use mysql or another sql?
Could you please tell me?

Thank You

Newest Book.png

Link to comment
Share on other sites

you can not do "insert where", "update where something=something AND somethingelse=somethingelse" will do the update, insert do not have that because your inserting and as SQLite does not guarantees order it does not matter where its inserted and, so "insert where" is impossible. + that will be illogical 

totally-illogical-spock.png

 

But you can allwayes remove before reinserting something if that is what you wish

DELETE FROM x WHERE id = 1;

INSERT INTO x VALUES (1, 'something', 'something', ...);

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

You can also use UPSERT, which means update if exists else insert. Use with caution !

All of this is common to almost every SQL engine I know of (Postgres, Mysql, sql server, oracle, ...). SQLite is by far the simplest to install and use.

Invest time to study SQL (w3school for instance) and SQLite documentation.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@Andreik@bogQ@jchd

I have a few more question

How to delete last rowid without knowing last rowid number?
I tried using this one and it worked

delete from Book where rowid = (SELECT max(rowid) FROM Book)

Is there easier way or another good alternatif?

----------------------------------------------------------------------------------------------------------------------------------------------------

How to use _SQLite_Display2DResult_SQLite?


After doing

$iRval = _SQLite_GetTable2D($hDatabase, 'SELECT * FROM Book', $aResult, $iRows, $iColumns)

I can continue with this below, the table is appeared
 

_ArrayDisplay($aResult)

But, I can't continue with this below, nothing happened, no table appeared

_SQLite_Display2DResult($aResult)


Thank You

Edited by HezzelQuartz
Link to comment
Share on other sites

Engine not having last row identifier only thing you can do is something similar to that what you already done 

Additional ways:

"WHERE rowid = (SELECT rowid FROM your_table ORDER BY rowid ESC LIMIT 1);" or "WHERE rowid IN (... );"

But i do think your way is the fastest and most easy to read

 

_SQLite_Display2DResult Returns or prints to Console a formated display of a 2Dimensional array

Display result to console (scite console for example) if $bReturn is properly set, so it will not popup window, if changed from default it will return data to your variable

Not tested it at the moment but to manage data you probably need to do something like this

$data = _SQLite_Display2DResult($aResult, 0, true)

 

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 
Link to comment
Share on other sites

@bogQ@Andreik@jchd and everyone
I have another question

How to select last 5 rowid?

I tried using this one and I Think it worked
 

SELECT * FROM ProductInformation where rowid between (SELECT max(rowid)-5 FROM ProductInformation) And (SELECT max(rowid) FROM ProductInformation)

Did I do anything wrong?
Is there easier way or another good alternatif so that the command can be more simple and easier to read?

Thank You

Link to comment
Share on other sites

Use this, provided "no" is an alias for "rowid" (that is if "no" is INTEGER PRIMARY KEY):

SELECT * FROM ProductInformation order by no desc limit 5;

If you insist on showing this resultset in ascending "no" order, use that:

select * from (SELECT * FROM ProductInformation order by no desc limit 5) order by no;

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 3 weeks later...

@Andreik@jchd@bogQ and everyone

Sorry, I want to ask again

From My table below, How to count how many "Pen" Appear in Category?

I tried using this command, and it worked

SELECT category, count(*) FROM Book where category = 'Pen'

1. Is there easier way or another good alternatif so that the command can be more simple and easier to read?

2. Could you please explain what (*) means?

3. What function in autoit should I use to insert that sqlite command above? _SQLite_QuerySingleRow?
 

Newest Book.png

Link to comment
Share on other sites

1) The SQL you use is the same number of words in your question. The SQL seems quite readable to me.

2) What if you read the SQLite documentation? https://www.sqlite.org/index.html

3) Yes. You ask for one row, so get one row.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Posted (edited)

@jchd@bogQ@Andreik and everyone

Sorry, I want to ask again

From My table below, how to get only 'product' column with 'Pen' Category into arrays?

I tried using this command, and it worked
 

_SQLite_GetTable2D($hDatabase, "SELECT Product from Book where Category = 'Pen'", $aResult, $iRows, $iColumns)

MsgBox(0, "Price of Book B", $aResult[1][0])
MsgBox(0, "Price of Book B", $aResult[2][0])
MsgBox(0, "Price of Book B", $aResult[3][0])
MsgBox(0, "Price of Book B", $aResult[4][0])

1. Is there easier way or another good alternatif so that the command can be more simple and easier to read?

2. Actually I prefer to use any function similar to _SQLite_QuerySingleRow, Can I use another function? maybe _SQLite_Query?

3. If _SQLite_Query can be used, I am confused. How to use that function? Could you all please give any idea, clue, or else?

Newest Book.png

Edited by HezzelQuartz
Link to comment
Share on other sites

For resultset with only one column and more than one row, use _SQLite_GetTable()

For resultset with more columns and more than one row, use _SQLite_GetTable2D()

Forget _SQLite_Query and friends.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

On 10/1/2024 at 3:48 PM, jchd said:

For resultset with only one column and more than one row, use _SQLite_GetTable()

For resultset with more columns and more than one row, use _SQLite_GetTable2D()

Forget _SQLite_Query and friends.

@jchd and anyone

How can I get how many rows are available in one column using either _SQLite_GetTable() or _SQLite_GetTable2D()?

Actually I can get total row using different command:
 

SELECT Product, count(*) FROM Book

But I really prefer to get row number using _SQLite_GetTable() or _SQLite_GetTable2D()?

Newest Book.png

Link to comment
Share on other sites

Read the documentation about these two functions.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...