parent
d302ca7340
commit
dc9b749c56
@ -1,53 +1,7 @@ |
|||||||
#include <iostream> |
#include<stdio.h> |
||||||
#include <mysql_driver.h> // MySQL Connector/C++库头文件 |
#include<string.h> |
||||||
#include <mysql_connection.h> |
void sqlQuery(char * name){ |
||||||
|
char sqlQuery[64] = "select * from cus where userid="; |
||||||
// 假设你已经有了一个sanitizeString函数,用于清理SQL注入风险
|
strcat(sqlQuery,name); |
||||||
std::string sanitizeString(const std::string& input) { |
strcat(sqlQuery,"'"); |
||||||
// 在这里实现SQL字符串清理逻辑
|
|
||||||
return cleanedInput; |
|
||||||
} |
|
||||||
|
|
||||||
int main() { |
|
||||||
try { |
|
||||||
sql::mysql::MySQL_Driver *driver; |
|
||||||
sql::Connection *con; |
|
||||||
|
|
||||||
// 初始化数据库连接
|
|
||||||
driver = sql::mysql::get_mysql_driver_instance(); |
|
||||||
con = driver->connect("tcp://127.0.0.1:3306", "username", "password"); |
|
||||||
con->setSchema("your_database"); |
|
||||||
|
|
||||||
std::string inputQuery = ""; |
|
||||||
std::cout << "请输入SQL查询语句: "; |
|
||||||
std::getline(std::cin, inputQuery); |
|
||||||
|
|
||||||
// 对输入的SQL语句进行验证和处理
|
|
||||||
std::string sqlQuery = sanitizeString(inputQuery); |
|
||||||
|
|
||||||
// 创建并执行SQL语句
|
|
||||||
sql::Statement *stmt = con->createStatement(); |
|
||||||
sql::ResultSet *res = stmt->executeQuery(sqlQuery); |
|
||||||
|
|
||||||
// 处理查询结果
|
|
||||||
while (res->next()) { |
|
||||||
// 从结果集中获取数据并进行处理
|
|
||||||
// 这里假设你知道第一列的名字,如果不是,请替换为实际列名
|
|
||||||
std::string resultData = res->getString("your_column_name"); |
|
||||||
std::cout << "查询结果: " << resultData << std::endl; |
|
||||||
} |
|
||||||
|
|
||||||
delete stmt; |
|
||||||
delete res; |
|
||||||
delete con; |
|
||||||
} |
|
||||||
catch (sql::SQLException &e) { |
|
||||||
std::cerr << "# ERR: SQLException in " << __FILE__; |
|
||||||
std::cerr << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl; |
|
||||||
std::cerr << "# ERR: " << e.what(); |
|
||||||
std::cerr << " (MySQL error code: " << e.getErrorCode(); |
|
||||||
std::cerr << ", SQLState: " << e.getSQLState() << " )" << std::endl; |
|
||||||
} |
|
||||||
|
|
||||||
return 0; |
|
||||||
} |
} |
Loading…
Reference in new issue