Re: [sqlite] database AND table already exist?

2012-07-20 Thread Igor Tandetnik
Black, Michael (IS)  wrote:
> Good pointso probably time equivalent either way.  Though table_info will 
> allow the feature creep of "does a column exist"
> pretty easily.

So would preparing a statement of the form "select ColumnToCheck from 
TableToCheck;"
-- 
Igor Tandetnik

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] database AND table already exist?

2012-07-20 Thread Black, Michael (IS)
Good pointso probably time equivalent either way.  Though table_info will 
allow the feature creep of "does a column exist" pretty easily.  Not that 
anybody ever adds requirements





Michael D. Black
Senior Scientist
Advanced Analytics Directorate
Advanced GEOINT Solutions Operating Unit
Northrop Grumman Information Systems


From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on 
behalf of Igor Tandetnik [itandet...@mvps.org]
Sent: Friday, July 20, 2012 7:59 AM
To: sqlite-users@sqlite.org
Subject: EXT :Re: [sqlite] database AND table already exist?

Black, Michael (IS) <michael.bla...@ngc.com> wrote:
> table_info() will be faster than doing "select *" I would think in most all 
> cases.

To check the existence of a table, you don't need to actually run the select 
statement - just prepare it and check for errors.
--
Igor Tandetnik

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] database AND table already exist?

2012-07-20 Thread Igor Tandetnik
Black, Michael (IS)  wrote:
> table_info() will be faster than doing "select *" I would think in most all 
> cases.

To check the existence of a table, you don't need to actually run the select 
statement - just prepare it and check for errors.
-- 
Igor Tandetnik

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] database AND table already exist?

2012-07-20 Thread Black, Michael (IS)
I needed a quick excercise this morning.  Never used table_info() before.
table_info() will be faster than doing "select *" I would think in most all 
cases.



#include 
#include 
#include 
#include "sqlite3.h"

using namespace std;

bool dbExists(string dbName) {
  sqlite3 *db;
  int rc = sqlite3_open_v2(dbName.c_str(), , SQLITE_OPEN_READONLY, NULL);
  if(rc != SQLITE_OK) {
return false;
  }
  rc = sqlite3_close(db);
  if(rc != SQLITE_OK) {
cerr << "Error on sqlite3_close??" << endl;
  }
  return true;
}

bool tableExists(string dbName, string table) {
  sqlite3 *db;
  int rc = sqlite3_open_v2(dbName.c_str(), , SQLITE_OPEN_READONLY, NULL);
  if(rc != SQLITE_OK) {
return false; // db doesn't exist
  }

  sqlite3_stmt *stmt;
  stringstream ss;
  ss << "pragma table_info(" << table << ");";
  rc = sqlite3_prepare_v2( db, ss.str().c_str() , -1, , NULL );
  if(rc != SQLITE_OK) {
cerr << "Error on sqlite3_prepare_v2: " << sqlite3_errmsg(db) << endl;
  }
  rc = sqlite3_step(stmt);
  if(rc != SQLITE_DONE && rc != SQLITE_ROW) {
cerr << "Error on sqlite3_step: " << sqlite3_errmsg(db) << endl;
  }

  bool myReturn = false;
  if (sqlite3_data_count(stmt) > 0) {
myReturn = true;
  }
  sqlite3_finalize(stmt);
  sqlite3_close(db);
  return myReturn;
}


Michael D. Black
Senior Scientist
Advanced Analytics Directorate
Advanced GEOINT Solutions Operating Unit
Northrop Grumman Information Systems




From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on 
behalf of Arbol One [arbol...@gmail.com]
Sent: Friday, July 20, 2012 5:51 AM
To: SqLite
Subject: EXT :[sqlite] database AND table already exist?


Is there a way to find out if a certain database AND table already exist?

In my C++ program I would like to query SQLite3 to find out if a database
already exists and also if a particular table exists in that database. Is
there a way to do this?

I am using std i/o methods to check for the existing SQLite3 file containing
the database, but I don't have a way to find out if the table in question
does in fact exist.



TIA



Freedom of speech does not translate to freedom of insulting



___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] database AND table already exist?

2012-07-20 Thread Simon Slavin

On 20 Jul 2012, at 11:51am, Arbol One  wrote:

> Is there a way to find out if a certain database AND table already exist?
> 
> In my C++ program I would like to query SQLite3 to find out if a database
> already exists and also if a particular table exists in that database. Is
> there a way to do this?

To find out if a database exists, use sqlite3_open_v2() and use the flag 
SQLITE_OPEN_READONLY as documented on



If the database doesn't exist, you'll get an error.  If the database does 
exist, it'll be opened and you can continue with

PRAGMA table_info(myTable)

as documented here



Simon.
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] database AND table already exist?

2012-07-20 Thread Donald Griggs
Darn.  I didn't mean to quote the table name: (though double quotes or
brackets are alllowed).   Sorry for the noise.

 SELECT 'anything'  FROM  MyTableInQuestion LIMIT 0;
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] database AND table already exist?

2012-07-20 Thread Donald Griggs
On Fri, Jul 20, 2012 at 6:51 AM, Arbol One  wrote:

> Is there a way to find out if a certain database AND table already exist?
>
> I am using std i/o methods to check for the existing SQLite3 file
> containing the database,


==> I think that's the standard method.


but I don't have a way to find out if the table in question
> does in fact exist.
>
> ==> You can
  - query sqlite_master
  - PRAGMA table_info
  - SELECT 'anything'  FROM 'MyTableInQuestion' LIMIT 0;
___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] database AND table already exist?

2012-07-20 Thread Arbol One
Is there a way to find out if a certain database AND table already exist?

In my C++ program I would like to query SQLite3 to find out if a database
already exists and also if a particular table exists in that database. Is
there a way to do this?

I am using std i/o methods to check for the existing SQLite3 file containing
the database, but I don't have a way to find out if the table in question
does in fact exist.

 

TIA  

 

Freedom of speech does not translate to freedom of insulting

 

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users