I can't Insert a new data to SQLITE
-
ok thanks for the info!
-
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 tested your code and it works fine. Did you forget to add QT += sql in your .pro file? -
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!
-
Qt doc:
bq. The Qt SQLite 2 plugin is offered for compatibility. Whenever possible, use the version 3 plugin instead.
-
If more than one instance of the application is running the database can be locked. Do not step into this trap as I did several times.
-
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.