简体   繁体   中英

how to insert data into sqlite using qt

how to insert the data into sqlite using qt :

query = QSqlQuery("INSERT INTO customer_details (rationcard_num, aadharcard_num, name) 
    values('"+rationcard_num+"','"+aadharcard_num+"','"+name+"')");

You can modify your code and make the query look like this, which I think will clarify your doubts:

Preparing query statement

QSqlQuery query;
query.prepare("INSERT INTO customer_details(rationcard_num \
                 , aadharcard_num \
                 , name) \ 
                 VALUES(:rationcard_num \
                 , :aadharcard_num \
                 , :name); ");

Bind values

query.bindValue(":rationcard_num", your_value);
query.bindValue(":aadharcard_num", your_value);
query.bindValue(":name", your_value);

DEBUGGING

You can use qDebug() to find if your query is executable:

qDebug()<<query.exec()<<endl;

This will return true if data inserted and false otherwise

You could probably call exec() on the query or call it directly with exec(const QString& query) . So for example you could do:

QSqlQuery query;
query.exec("INSERT INTO customer_details (rationcard_num, aadharcard_num, name)" 
            + "values('" + rationcard_num + "','" + aadharcard_num + "','" 
            + name + "')");

If however you want to use a prepared statement you would want to construct the query with a QSqlDatabase, call prepare(statement) on that and bind the values. For example:

QSqlQuery query;
query.prepare("INSERT INTO person (rationcard_num, aadharcard_num, name) "
               "VALUES (:rationcard_num, :aadharcard_num, :name)");
query.bindValue(":rationcard_num", rationcard_num);
query.bindValue(":aadharcard_num", aadharcard_num);
query.bindValue(":name", name);
query.exec();
QSqlQuery query;
query.exec("INSERT INTO customer_details (rationcard_num,aadharcard_num,name)VALUES('"+rationcard_num+"','"+aadharcard_num+"','"+name+")");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM