Jump to content

SQLite - editing an existing database


Bert
 Share

Recommended Posts

I have an existing database called test.db. I know there is a column called "Name" and a column called "Value". There are about 51 rows of data. I've been poring through the help file, goggling like mad and I'm no closer to being able to work with this thing. Outside of today I have about zero experience with SQLite. I know after this project is done I may not work with again for a long time due to other projects in the pipeline.

What I'm looking to do is simply delete some of the data in the values column. In simply trying to run a query I keep running into an error:

no such table

Example script:

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

Local $hQuery, $aRow, $aNames

_SQLite_Startup()
If @error Then
    MsgBox(16, "SQLite Error", "SQLite3.dll Can't be Loaded!")
    Exit -1
EndIf
ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)

Local $sDbName =   "test.db" ;DB is in the same folder as the script
Local $hDskDb = _SQLite_Open($sDbName) ; Open a permanent disk database - this works fine
_SQLite_Query(-1, "SELECT ROWID,1 FROM Test ORDER BY a;", $hQuery) ;what am I doing wrong here? I get no such table: Test, Name, Value...what do I use here?

If @error Then
    MsgBox(16, "SQLite Error", "Can't open or create a permanent Database!")
    Exit -1
EndIf

; we can use the 3 database as needed by refering to their handle

; close the Dbs we created, in any order
_SQLite_Close($hDskDb)

; we don't really need that DB
FileDelete($sDbName)

_SQLite_Shutdown()

output from the console when I run the script:

_SQLite_LibVersion=3.7.2

! SQLite.au3 Error

--> Function: _SQLite_Query

--> Query: SELECT ROWID,1 FROM Test ORDER BY a;

--> Error: no such table: Test

!>15:08:53 AutoIT3.exe ended.rc:-1

>Exit code: -1 Time: 7.006

What am I doing wrong here?
Link to comment
Share on other sites

  • Moderators

You could use something like this to find out the names of all your tables:

SELECT name FROM test
WHERE type='table'
ORDER BY name;

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Hi MPH,

Please run the following (after opening a connection with handle $hdb, obviously):

_SQLite_GetTable2d($hdb, "select * from sqlite_master where type = 'table'", $aTables)
_ArrayDisplay($aTables)

You can then see which tables are there and how they were declared (the SQL column).

Edit: JLogan, your query won't work in SQLite.

Edited by jchd

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

I was just going to offer something to that effect, but had to look it up first.

If you have the sqlite3.exe program, you can do the following.

cmdprompt> sqlite3 test.db

sqlite> .tables
 ... tables names are listed here
sqlite> .exit

cmdprompt>

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

  • Moderators

I was just updating mine, but jhcd beat me to it. If you don't know the table name, you have to go through the sqlite_master.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Correct. That's where SQLite stores the schema. Granted, it isn't in the most practical form (have to reparse SQL to get full details by program but it's better than nothing).

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

Hi MPH,

Please run the following (after opening a connection with handle $hdb, obviously):

_SQLite_GetTable2d($hdb, "select * from sqlite_master where type = 'table'", $aTables)
_ArrayDisplay($aTables)

You can then see which tables are there and how they were declared (the SQL column).

Edit: JLogan, your query won't work in SQLite.

OK, tried your code. I got a results back and tried each item listed. Still getting the same error : no such table.

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

Local $hQuery, $aNames, $aTables
Local $iRows, $iColumns, $sMsg

_SQLite_Startup()
If @error Then
    MsgBox(16, "SQLite Error", "SQLite3.dll Can't be Loaded!")
    Exit -1
EndIf
ConsoleWrite("_SQLite_LibVersion=" & _SQLite_LibVersion() & @CRLF)

Local $sDbName =   "test.db" ;DB is in the same folder as the script
Local $hDskDb = _SQLite_Open($sDbName) ; Open a permanent disk database - this works fine
;_SQLite_GetTable($hDskDb, "select * from sqlite_master where type = 'table'", $aTables, $iRows, $iColumns)
;_ArrayDisplay($aTables)
;returned - [0]|type|name|tbl_name|rootpage|sql

;_SQLite_GetTable2d($hDskDb, "select * from sqlite_master where type = 'table'", $aTables, $iRows, $iColumns)
;_ArrayDisplay($aTables)
#cs
Returned:
[0]|5
[1]|type
[2]|name
[3]|tbl_name
[4]|rootpage
[5]|sql
#ce

_SQLite_Query(-1, "SELECT ROWID,1 FROM NAME ORDER BY Name;", $hQuery) ;still doesn't work.

If @error Then
    MsgBox(16, "SQLite Error", @error)
    Exit -1
EndIf

While _SQLite_FetchData($hQuery, $iRows) = $SQLITE_OK
    $sMsg &= $iRows[0]
WEnd
_SQLite_Exec(-1, "DROP TABLE aTest;") ; Remove the table
MsgBox(0, "SQLite", "Get Data using a Query : " & $sMsg)

_SQLite_Close($hDskDb)
FileDelete($sDbName)
_SQLite_Shutdown()


#cs
_SQLite_LibVersion=3.7.2
!   SQLite.au3 Error
--> Function: _SQLite_Query
--> Query:    SELECT ROWID,1 FROM NAME ORDER BY Name;
--> Error:    no such table: NAME

!>08:44:51 AutoIT3.exe ended.rc:-1
>Exit code: -1    Time: 5.749
#ce
Edited by MPH
Link to comment
Share on other sites

Why not open it in an sqlite browser?

There's a firefox add-on called SQlite manager, which i quite like. Others exist.

You could use this to find out the table structure so you can write programmed edits etc in the future - or use its editing facility to make the occasional ad hoc edit that's no worth writing script for.

William

Link to comment
Share on other sites

If you don't get anything back but only headers, it means the DB does actually contain no table, as the first item (table count) of the _SQLite_GetTable shows. The file size doesn't mean much as when dropping a table, pages that were freed are not automatically vaccumed.

There's nothing you can do to reliably "undrop" table(s).

Lastly: stay away of the _SQLite_Query, _SQlite_Step, _SQLite_FetchData sequence. First you have a lot of error handling to code and you also have to finalize the statement. Favor _SQLite_GetTable[2d] instead in a single operation.

Edited by jchd

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

If it's something you can share, even privately, I'd be very pleased to help.

If that's private stuff, I warmly recommend you start by downloading and installing SQLite Expert Personal. From there, open your DB and look around. Don't edit things inside without knowing exactly what to do.

FYI, in the existing application, _SQLite_Open should have the existing DB as parameter. Then it won't create a new one. Then it's possible that the app drops tables or does cleanup activities. May be you want to skip that. Anyway, work on a copy.

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...