[英]Inserting a variable into a mysql table
I know how to insert non variable values into a MySql table (using C++): 我知道如何将非变量值插入MySql表(使用C ++):
Ex: 例如:
//Table person:
mysql_query(conn, "INSERT INTO MyTable VALUES ( '1', 'John', 'Kennedy')");
mysql_query(conn, "INSERT INTO MyTable VALUES ( '2', 'Dave', 'Chappelle')");
mysql_query(conn, "INSERT INTO MyTable VALUES ( '3', 'Arnold', 'Schwarzenegger')");
//Up to 100 rows of data...
What I would like to do is be able to insert a variable into a table with C++: 我想要做的是能够使用C ++将变量插入到表中:
Ex: 例如:
i=0;
for(i=0; i < 100; i++)
{
mysql_query(conn, "INSERT INTO MyTable VALUES ( i, 'FirstName', 'LastName')");
}
Rather than having to manually type in 100 rows of data, I would like to be able to use a for loop, and increment a variable to create the rows for me. 我不想手动输入100行数据,而是希望能够使用for循环,并增加一个变量来为我创建行。
Does anyone know how to insert a variable into a mysql database using C++? 有谁知道如何使用C ++将变量插入到mysql数据库中?
I am not too familiar with the syntax to get it to work, but the generic version of what you want is Prepared Statements . 我不太熟悉使其工作的语法,但您想要的通用版本是Prepared Statements 。 You make a statement which has variables as placeholders.
您创建一个包含变量作为占位符的语句。 These are later provided values and used.
这些是以后提供的值并使用。
Edit: You can find more information about prepared statement use in c++ at the MySQL website 编辑:您可以在MySQL网站上找到有关在c ++中使用预准备语句的更多信息
// ...
sql::Connection *con;
sql::PreparedStatement *prep_stmt
// ...
prep_stmt = con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)");
prep_stmt->setInt(1, 1);
prep_stmt->setString(2, "a");
prep_stmt->execute();
prep_stmt->setInt(1, 2);
prep_stmt->setString(2, "b");
prep_stmt->execute();
delete prep_stmt;
delete con;
You can do it by 你可以做到
int var = 10;
string str = to_string(var);
string requete="INSERT INTO stat(temps) VALUES (\"";
requete += str;
requete += "\")";
mysql_query(&mysql,requete.c_str());
just specify in mySql that the field has a type of int , double , float etc. 只需在mySql中指定该字段的类型为int,double,float等。
You need to make the query string first before performing a query. 在执行查询之前,您需要先创建查询字符串。 Replace the below printf with a string class instance to make one.
用字符串类实例替换下面的printf来制作一个。 You may want to put it in a vector of strings also.
您可能还想将它放在字符串向量中。
char queryFmt[]="INSERT INTO myTable VALUES('%d','%s',%s)\n";
char color[16];
char fruit[32];
for(int i=0;i<2;i++)
{
//scanf_s("%s",color);
//scanf_s("%s",fruit);
std::cin>>color;
std::cin>>fruit;
printf_s(queryFmt,i,color,fruit);
}
Please refer to many a thread of the related issues using scanf to read in a character or string inside a loop if you don't know yet and are interested in any. 如果您还不知道并且对任何问题感兴趣,请使用scanf参考许多相关问题的线程来读取循环内的字符或字符串。
this worked for me. 这对我有用。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
sql::Driver *driver;
sql::Connection *con;
sql::ResultSet *res;
sql::Statement *stmt;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "admin", "password");
// db name
con->setSchema("db_name");
std::string lookup = "bob" ;
stmt = con->createStatement();
res = stmt->executeQuery("SELECT last_name FROM names_table WHERE first_name = '" + lookup + "'");
while (res->next()) {
std::cout << res->getString("last_name") << std::endl;
}
delete res;
delete stmt;
delete con;
i=0;
for(i=0; i < 100; i++)
{
mysql_query(conn, "INSERT INTO MyTable VALUES ( '"+i+"', 'blue', 'pumpkin')");
}
This should work 这应该工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.