Jump to content
Sign in to follow this  
rony2006

How to make a simple database?

Recommended Posts

rony2006

Hello, I am beginner in Autoit and i want to create, read, edit and delete a simple database.

For exemple I have the default database:

1        Tom       1000

2        Bill         1200

3        Marian    2000

And i have

$variabile1 = 200

$variabile2 = 300 

$variabile3 =250

Next, i want to add the value of variabile1 to 1000 and then the database to look something like:

 

1        Tom       1200

2        Bill         1200

3        Marian    2000

And last thing i want to $var1 to have the new values of row 1 for ex $var1 = 1  Tom  1200 

How can i do this in the most simple way? Can you give me some exemple? If is posibile i want to use a .txt file for the database, but i think do it also with sqlite but i dont know how to do it.

Thank you!

Share this post


Link to post
Share on other sites
Danyfirex

Look into help file.

#include <SQLite.au3>
#include <SQLite.dll.au3>

Local $aResult, $iRows, $iColumns, $iRval

_SQLite_Startup()
If @error Then
    MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")
    Exit -1
EndIf
ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)
_SQLite_Open() ; Open a :memory: database
If @error Then
    MsgBox(16, "SQLite Error", "Can't Load Database!")
    Exit -1
EndIf

;Example Table
;   ID  | Name   | Age
;   -----------------------
;    1     Alice  | 43
;    2     Bob    | 28
;    3     Cindy  | 21

If Not _SQLite_Exec(-1, "CREATE TEMP TABLE persons (ID,Name, Age);") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES (1,'Alice','43');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES (2,'Bob','28');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES (3,'Cindy','21');") = $SQLITE_OK Then _
        MsgBox(16, "SQLite Error", _SQLite_ErrMsg())

; Query
$iRval = _SQLite_GetTable2d(-1, "SELECT * FROM persons;", $aResult, $iRows, $iColumns)
If $iRval = $SQLITE_OK Then
    _SQLite_Display2DResult($aResult)

;~    $aResult looks like this:
;~
;~  ID Name   Age
;~  1 Alice  43
;~  2 Bob    28
;~  3 Cindy  21
;~
;~    If the dimensions would be switched in _SQLite_GetTable2d the result would look like this:
;~
;~   Name  Alice  Bob  Cindy
;~   Age   43     28   21

Else
    MsgBox(16, "SQLite Error: " & $iRval, _SQLite_ErrMsg())
EndIf

_SQLite_Close()
_SQLite_Shutdown()

regards

  • Like 2

Share this post


Link to post
Share on other sites
rony2006

Thanks, but if i run that code, were is the database created?

I want for ex to make the database in c:/database.db and work on it.

Share this post


Link to post
Share on other sites
Danyfirex

Look _SQLite_Open in the example code.

#include <SQLite.au3>
#include <SQLite.dll.au3>

Local $aResult, $iRows, $iColumns, $iRval

_SQLite_Startup()
If @error Then
    MsgBox(16, "SQLite Error", "SQLite.dll Can't be Loaded!")
    Exit -1
EndIf
ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)
_SQLite_Open() ; Open a :memory: database
If @error Then
    MsgBox(16, "SQLite Error", "Can't Load Database!")
    Exit -1
EndIf

Local $hDskDb = _SQLite_Open(@ScriptDir & '\my.db') ;this is the path ; Open a permanent disk database
If @error Then
    MsgBox(16, "SQLite Error", "Can't open or create a permanent Database!")
    Exit -1
EndIf

;Example Table
;   ID  | Name   | Age
;   -----------------------
;    1     Alice  | 43
;    2     Bob    | 28
;    3     Cindy  | 21


;~ If Not _SQLite_Exec(-1, "CREATE TABLE persons (ID,Name, Age);") = $SQLITE_OK Then _
;~         MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
;~ If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES (1,'Alice','43');") = $SQLITE_OK Then _
;~         MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
;~ If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES (2,'Bob','28');") = $SQLITE_OK Then _
;~         MsgBox(16, "SQLite Error", _SQLite_ErrMsg())
;~ If Not _SQLite_Exec(-1, "INSERT INTO persons VALUES (3,'Cindy','21');") = $SQLITE_OK Then _
;~         MsgBox(16, "SQLite Error", _SQLite_ErrMsg())

; Query
$iRval = _SQLite_GetTable2d(-1, "SELECT * FROM persons;", $aResult, $iRows, $iColumns)
If $iRval = $SQLITE_OK Then
    _SQLite_Display2DResult($aResult)

;~    $aResult looks like this:
;~
;~  ID Name   Age
;~  1 Alice  43
;~  2 Bob    28
;~  3 Cindy  21
;~
;~    If the dimensions would be switched in _SQLite_GetTable2d the result would look like this:
;~
;~   Name  Alice  Bob  Cindy
;~   Age   43     28   21

Else
    MsgBox(16, "SQLite Error: " & $iRval, _SQLite_ErrMsg())
EndIf

_SQLite_Close()
_SQLite_Shutdown()

saludos

Share this post


Link to post
Share on other sites
TheSaint

If you want the simplest of ways, then check out IniWrite and the other ini commands.

Some will tell you that INI files are redundant, but I've seen very little evidence of that ... and they've been saying that for years.

They have their benefits ... especially manual edits with Notepad, size and portability.

  • Like 3

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
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
Sign in to follow this  

×