PDA

View Full Version : Can't create table



waynew
28th October 2009, 23:26
Can anyone see why the following code is not creating the table?



#include <iostream>
#include <QString>
#include <QtSql>
#include "stninfodialog.h"
#include <QDialog>

bool makeStnDB() {
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("StnInfo.sqlite");
db.open();

//Check to see if stninfo exists...
QSqlQuery query = QSqlQuery::QSqlQuery("StnInfo.sqlite");
query.exec("SELECT call FROM stnInfo");

if (query.isNull(1)) {
query.exec("CREATE table stnInfo (id int recNum primary key, call varchar(8), name varchar(80),"
" address1 varchar(80), accress2 varchar(80), city varchar(80), state varchar(80), state varchar(40),"
" postalCode varchar(20), country varchar(40), grid varchar(8), cqZone varchar(8), ituZone varchar(8),"
" latitude varchar(8), longitude varchar(8), licenseClass varchar(12))");

StnInfoDialog *StnInfoD = new StnInfoDialog();
StnInfoD->show();

}
if (!db.open()) {
return false;
}
return true;
}


It does return true.
I also tried it without the check to see if stninfo exists and still no luck.

briang
29th October 2009, 06:46
typo line 14 ????????
select all from???

briang

waynew
29th October 2009, 11:34
Thanks for trying Briang.
Nope, "call" is the name of the second column in the table.
If you wanted all columns in sql, you would select * .

waynew
30th October 2009, 02:06
I now know the source of the problem, but not how to fix it yet.
It fails to create the table when the create table statement extends beyond one line of code.

For example, this works ok and creates a table:



query.exec("CREATE table stnInfo(id int primary key,call varchar(8),name varchar(80))");


Any ideas anyone?

jiaco
30th October 2009, 07:36
Why not put the create table string into a QString first, maybe even a const QString in the header.

jano_alex_es
30th October 2009, 10:19
by the way, whu don't you write it in only one line? does exec() give you true or false?

Rembobo
30th October 2009, 11:17
Use command line sqlite or check for any error after creating table.

Enter SQL statements terminated with a ";"
sqlite> CREATE table stnInfo (id int recNum primary key, call varchar(8), name varchar(80), addr
ess1 varchar(80), accress2 varchar(80), city varchar(80), state varchar(80), state varchar(40),
postalCode varchar(20), country varchar(40), grid varchar(8), cqZone varchar(8), ituZone varchar
(8), latitude varchar(8), longitude varchar(8), licenseClass varchar(12));
SQL error: duplicate column name: state
sqlite>

waynew
30th October 2009, 23:13
Thanks Rembobo, my eyes must be getting bad. Typos are the pits.
Works fine now. :)