Sign in to follow this  
Followers 0
rahuroy

How to verify existence of sql table

8 posts in this topic

Hi ...I have to drop sql database table.But below script works fine if the mentioned database exists.But scripts fails if db table doesnt exist and throws error.

How to verify the existence of database table using autoit? If it exists then only drop that table

$sqlCon = ObjCreate("ADODB.Connection")

$sqlCon.Open("Provider=SQLOLEDB;Data Source= " & $sqlserver & ";Trusted_Connection=Yes;")

;$sqlCon.Execute("DROP DATABASE " & $emdbname)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I think there is an index of all existing tables (and maybe some other stuff) in the table "sqlite_master".Just noticed this is probably only true for SQLite. perhaps there is an equivalent for SQL. I've never used it.

Edited by Tvern

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

rahuroy,

You can "drop" a database or "delete" a table using ADOX. Your post is slightly contradictory as to exactly what you want to do.This code fragment is how I delete tables before recreating them:

if tblexist('Games') then
        $cat.tables.delete('Games')
        showl('Delete table = Games')
    endif

And the function:

func tblexist($t)

    local $cat = ObjCreate("ADOX.Catalog")
    $cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $db & "; Jet OLEDB:Engine Type=5;"

    For $tbl In $cat.Tables
        If $tbl.Name = $t then
            $cat = 0
            return true
        Else
            $cat = 0
            return false
        endif
    Next

EndFunc

The environment is WinXP / Office2002. The code was translated from VBScript when I junked that Edsel.

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I believe you can always do something along the line of standard SQL:

drop table if exists mytbl;

Should your DB engine still produce error (why should it?), then try:

create table mytbl if not exists; -- insert dummy column parameters if needed

drop table mytbl;

This way that the table already exists or not becomes unimportant and drop shouldn't throw any error at your face.

@kylomas: you drop a table, an index, a view, a trigger but you delete rows from a table.

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)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

jhcd,

Yes, if you are operating within the DB. IF you are at the catalog level (ADOX) then the example that I posted above works. At least, that's my understanding of it. The above post was adapted from some MS doc for VBScript.

kylomas

Edit: See this thread - http://www.google.com/url?sa=t&source=web&cd=1&sqi=2&ved=0CBMQFjAA&url=http%3A%2F%2Fblogs.technet.com%2Fb%2Fheyscriptingguy%2Farchive%2F2005%2F03%2F11%2Fhow-can-i-delete-a-table-from-a-jet-database.aspx&ei=e7FZTbOtA4autwfl9q3zDA&usg=AFQjCNFm-iZLXbeeD71At4iODx6yqB7MNg

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Yeah you're right, but that's a Microsoftism.

Since this isn't portable at all, better stick to basic SQL.


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)

Share this post


Link to post
Share on other sites

jhcd,

Precicely why I'm trying to get off M$ (going to SQLite).

@rahuroy - jhcd is right. Better to use a consistent approach (distinctly un-M$ like).

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

The following script I've used to check tables.

HTH, Reinhard

$oDb.Open("SQL-Database") ;ODBC-DSN defined Database Connection

func Sql_table()
    Const $adTables = 20  ;description at: http://www.w3schools.com/ADO/met_conn_openschema.asp#schemaenum
    local $list
    $oRec = $odb.OpenSchema($adTables)
    while not $oRec.eof
        If StringLen( $oRec("TABLE_TYPE").value) > 5 Then;; Skip the hidden internal tables
                $oRec.movenext
                ContinueLoop
        EndIf
        $list = $list & $oRec("TABLE_NAME").value & ";"
        $oRec.MoveNext
    wend
    MsgBox(0,"",$list)

EndFunc
Edited by ReFran

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  
Followers 0