Now, I have the C code to check if a table exists:
#include <stdio.h>
#include "sqlite3.h"
int main(int argc, const char *argv[]){
sqlite3 *db;
sqlite3_stmt *stmt;
int rc = 0;
int hasTable = 0;// flag for the result
rc = sqlite3_open("test.db", &db);
if( rc ){
printf("cannot open database");
}else{
rc = sqlite3_prepare_v2(db, "select count(*) from sqlite_master
where
type='table' and lower(name)=lower('tablename')", -1, &stmt, 0);
if( !rc ){
rc = sqlite3_step(stmt);
if( rc==SQLITE_ROW ) {
hasTable = (int)sqlite3_column_int(stmt, 0);
printf("Table tablename %s in test.db\n",
hasTable ? "exists" : "does
not exist");
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
}
It works. And there may be a easier way to do this.
Peter Aronson-3 wrote:
>
> Er, what do you mean by C Syntax -- SQL isn't C? If you meant ANSI SQL
> syntax,
> you could use:
>
> select count(*) from sqlite_master where type='table' and
> lower(name)=lower('tablename');
>
> Instead. But since you're accessing a metadata table that only exists in
> SQLite, this isn't particularly more standard.
>
> Peter
>
>
>
> ________________________________
> From: Wei Song 2 <[email protected]>
> To: [email protected]
> Sent: Fri, March 9, 2012 12:36:58 PM
> Subject: Re: [sqlite] Building an SQLite Extension: How to check if a
> table
> exists in the database?
>
>
> I'd like get the result in C Syntax. How can I do it?
>
>
> Peter Aronson-3 wrote:
>>
>> You got to be a bit careful there, SQLite isn't case-sensitive about
>> table
>> names, but sqlite_master will preserve the case from the CREATE TABLE
>> statement. Instead of
>>
>>
>> select count(*) from sqlite_master where type='table' and
>> name='tablename';
>>
>> You need something like
>>
>> select count(*) from sqlite_master where type='table' and
>> name='tablename'
>> collate nocase;
>>
>> Peter
>>
>>
>>
>> ________________________________
>> From: Roger Andersson <[email protected]>
>> To: [email protected]
>> Sent: Fri, March 9, 2012 11:52:31 AM
>> Subject: Re: [sqlite] Building an SQLite Extension: How to check if a
>> table
>> exists in the database?
>>
>> On 03/09/12 19:39, Wei Song wrote:
>>> Hello,
>>>
>>> I'm developing an SQLite extension which uses a function to set data
>>> into
>>> a
>>>table. I'd like to know how to check if a table exists in a database?
>>>
>> It's hard to say what you need but maybe
>> select count(*) from sqlite_master where type='table' and
>> name='tablename';
>> /Roger
>> _______________________________________________
>> sqlite-users mailing list
>> [email protected]
>> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>> _______________________________________________
>> sqlite-users mailing list
>> [email protected]
>> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>>
>>
>
> --
> View this message in context:
> http://old.nabble.com/Building-an-SQLite-Extension%3A-How-to-check-if-a-table-exists-in-the-database--tp33473784p33474119.html
>
> Sent from the SQLite mailing list archive at Nabble.com.
>
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>
>
--
View this message in context:
http://old.nabble.com/Building-an-SQLite-Extension%3A-How-to-check-if-a-table-exists-in-the-database--tp33473784p33480624.html
Sent from the SQLite mailing list archive at Nabble.com.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users