• 12926阅读
  • 5回复

急,qt操作MYSQL,可查询,但是无法插入。 [复制链接]

上一主题 下一主题
离线haoyue
 

只看楼主 倒序阅读 楼主  发表于: 2011-03-30
一、
            query.prepare("INSERT INTO mobileinfo (MobileNo, MobileName, MobileUserID, SEX, MobileArea) VALUES (?, ?, ?, ?, ?)");
            query.addBindValue(1);
            query.addBindValue(1);
            query.addBindValue(1);
            query.addBindValue(1);
            query.addBindValue(1);
            query.exec();
                                                qDebug() << query.lastError();
如果我这样编写,qdebug返回的信息为:QSqlError(2036, "QMYSQL3: Unable to bind value", "Using unsupported buffer type: 6488161  (parameter: 1)")

如果采用下面的
二、            query.exec("INSERT INTO mobileinfo (MobileNo, MobileName, MobileUserID, SEX, MobileArea) " "VALUES (0, 0, 65000, 0, 0)");
则正常运行。

问题是,如果我采用二这个方法,我要如何把变量值在SQL里面传递进去呢?

我的QT版本为4.7.MYSQL版本为5.1。请教各位了。
离线haoyue

只看该作者 1楼 发表于: 2011-03-30
从QT的帮助文档里面,我发现有这样的一段话
QMYSQL for MySQL 4 and higher
QMYSQL Stored Procedure Support
MySQL 5 introduces stored procedure support at the SQL level, but no API to control IN, OUT and INOUT parameters. Therefore, parameters have to be set and read using SQL commands instead of QSqlQuery::bindValue().
Example stored procedure:
create procedure qtestproc (OUT param1 INT, OUT param2 INT)
BEGIN
     set param1 = 42;
     set param2 = 43;
END
Source code to access the OUT values:
QSqlQuery q;
q.exec("call qtestproc (@outval1, @outval2)");
q.exec("select @outval1, @outval2");
q.next();
qDebug() << q.value(0) << q.value(1); // outputs "42" and "43"
Note: @outval1 and @outval2 are variables local to the current connection and will not be affected by queries sent from another host or connection.

看了半天,我还是不知道要如何把变量值传递给SQL。请教各位了。
可以给出解决:
QSqlError(2036, "QMYSQL3: Unable to bind value", "Using unsupported buffer type: 6488161  (parameter: 1)")
或者 传递变量值进去给SQL方法都可以。

麻烦各位啦。描述如果有不清楚的,请麻烦跟帖,谢谢
离线wxj120bw

只看该作者 2楼 发表于: 2011-03-31
楼主可以尝试下 addBindValue的参数还是手动写成QVariant类型
离线haoyue

只看该作者 3楼 发表于: 2011-03-31
修改为            query.prepare("INSERT INTO mobileinfo (MobileNo, MobileName, MobileUserID, SEX, MobileArea) VALUES (?, ?, ?, ?, ?)");
            query.addBindValue(QVariant("a"));
            query.addBindValue(QVariant("a"));
            query.addBindValue(QVariant("a"));
            query.addBindValue(QVariant("a"));
            query.addBindValue(QVariant("a"));
            query.exec();
还是提示QSqlError(2036, "QMYSQL3: Unable to bind value", "Using unsupported buffer type: 168626701  (parameter: 1)")

我想问的是QT4和MYSQL5,到底要如何传递传入进去呢?
离线qq723947150
只看该作者 4楼 发表于: 2011-03-31
如果采用下面的
二、            query.exec("INSERT INTO mobileinfo (MobileNo, MobileName, MobileUserID, SEX, MobileArea) " "VALUES (0, 0, 65000, 0, 0)");
则正常运行。
问题是,如果我采用二这个方法,我要如何把变量值在SQL里面传递进去呢?


你可以试下这个方法(假设a,b,c,d,e是相应的变量)
query.exec(tr("INSERT INTO mobileinfo (MobileNo, MobileName, MobileUserID, SEX, MobileArea)  VALUES ('%1', '%2', '%3', '%4', '%5')").arg(a,b,c,d,e));
Qt中文论坛技术总群:135888831(2000人超级大群)
Qt中文论坛技术总群1:55089444(满)
Qt中文论坛技术总群2:68150567 (未满)
Qt菜鸟①群:100797515 (满)
Qt菜鸟②群:127888133(未满)
Qt精英群:13148934 (未满)
Qt for Symbian:135888829
Qt for iPhone:135888830
Qt for Android:135888831
Qt for Linux:135888832
主页:http://user.qzone.qq.com/723947150
离线ken_d
只看该作者 5楼 发表于: 2011-03-31
这就是一个SQL 的INSERT命令,字符串拼接替掉问号不就好了么。SQL最终接受的是命令。
快速回复
限100 字节
 
上一个 下一个