PDA

View Full Version : How to find that, i am creating database first time



newtowindows
27th October 2009, 12:14
Hi...I am building a qt(4.5.3) application which uses sqlite database. My sample code is

QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("sample.db");
db.open();

Here db.open() function opens my database, creates new databas(sample) if didnt existed.
Any how it returning db handle. But my issue is, if i am creating database first time i need to create some tables. If database already existed then need to read those tables. If i am using db.open() function it returning a valid db handle but how can i know that my database is created just now are it has been created before. please give me a solution.( i am new to qt)

spirit
27th October 2009, 12:18
use CREATE TABLE IF NOT EXISTS statement when tables are being created.

newtowindows
27th October 2009, 12:33
I use this solution in my cae...let me explain exactly what i am looking for..
case 1:
1) user starts the application. user will see a login screen(user id , password)
If user is allready using application( not launching first time ) then login credentials can be compared with the existing values which are all ready stored in a table( say table name LOGINTB) for authentication. simply i will try to read the table values in my db.

case 2:
2) My problem is here...
If user launching my application first time he wont get login screen. he has to get a screen which asks user to create user id and password and some personal details. But in order show this screen i have know there is nothing such table called LOGINTB in my db.
we can create that table if not existed but how can i spot table not existed before..bacause i need to show other than login screen if user launching my app first time

I hope you understand my prob. Please reply me if you require any further information.

newtowindows
27th October 2009, 12:50
a typo mistake ..
I use this solution in my cae/-->I cannot use this solution in my case
please read my explination above

spirit
27th October 2009, 14:33
try to check if database is opened using QSqlDatabase::isOpen or QSqlDatabase::isValid and if database is not valid/opened then show dialog and create database.