I can't Insert a new data to SQLITE
-
[quote author="wenks21" date="1305615786"]It only connects to the database.[/quote]
So... after the "Connected." output, you get nothing else? Your code suggests that you should either get an error or an OK for both the queries you try to execute. If you get an error, please show it.Edit:
I think you are messing up the constructors of your QSqlQuery object. Try to use the constructor that takes a QSqlDatabase argument. -
You might try the QSqlQuery documentation to learn about the different constructors for that class...
If you get no other output, you might try to set a breakpoint at the place where you last get output, and follow the program execution statement by statement by running in debug mode (F5 in Qt Creator).
-
Hi wenks21,
Did you resolve your problem?
I am in the same point... :( -
You need to open the actual file before opening the database within, like so....
@
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QFile newdbfile(QDir::toNativeSeparators(QDir::homePath()+"/test.sqlt"));
db.setDatabaseName(newdbfile.fileName());if (newdbfile.open(QIODevice::ReadWrite)) { if(!db.open()) { qFatal("Failed to connect!"); } else qDebug("Connected."); }
@
notice, that I wrapped your db.open() with a newdbfile.open().
also, this little dance I did sure makes it easy to move around 'tween machines and platforms
@QFile newdbfile(QDir::toNativeSeparators(QDir::homePath()+"/test.sqlt")); db.setDatabaseName(newdbfile.fileName());
@
I built and ran this code inserted into your code. It works.
-
[quote author="solareclectic" date="1309313429"]You need to open the actual file before opening the database within, like so....[/quote]
You do not need to open the file. If the code above does not work it is most likely that you
- have no permission to write to the C:/sqlite3 directory
- have no directory C:/sqlite3, which prevents the QSQLITE driver from creating the file (this might be the reason why opening - and thus creating - the file solves your problem)
-
Hi,
I have the same error with solareclectic code, The db is created and after 'Connected' output I don't get nothing, the table is not created.
Yes, I added QT += sql in my .pro file.
There is something I omitted because I didn't give importance and it is that I use QSQLITE2 driver instead of QSQLITE, could this be the error?
Thanks!
-
Solved.
Using QSQLITE works perfectly. With QSQLITE2 doesn't.
Thanks a lot!
-
@wenks21 said:
qry.prepare("insert into sample1.db (firstname, lastname) values ('John','Dean')");
check this above line in your code:
The sql query should be : insert into table_name(column_name,column_name)values (value,value);Your are missing out the table_name instead adding your db name. That's wrong.