vs2005中使用mysql数据库
try{sql::Driver*driver;/*Createaconnection*/driver=get_driver_instance();con=driver->...
try {
sql::Driver *driver;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "srh", "123");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS tests");
stmt->execute("CREATE TABLE tests(id INT, ip VARCHAR(45),time DATETIME)");
delete stmt;
/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO tests(id,ip,time) VALUES (?,?,?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->setString(2, "12345");
pstmt->setString(3, "20110420104820");
pstmt->executeUpdate();
}
delete pstmt;
/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id,ip FROM tests ORDER BY id ASC");
res = pstmt->executeQuery();
/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
{
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
cout << "\t... MySQL counts: " << res->getString("ip") << endl;
}
delete res;
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "End"<< endl;
}
执行到pstmt->executeUpdate();
这一句是,程序抛出异常,而且保存的内容也为0;
研究了很久也不知道是什么原因。
想请教各位大侠怎么样才能正确保存数据呢?
好吧 我知道原因了,一个dll文件没有链接上去。麻烦各位了 展开
sql::Driver *driver;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "srh", "123");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
stmt->execute("DROP TABLE IF EXISTS tests");
stmt->execute("CREATE TABLE tests(id INT, ip VARCHAR(45),time DATETIME)");
delete stmt;
/* '?' is the supported placeholder syntax */
pstmt = con->prepareStatement("INSERT INTO tests(id,ip,time) VALUES (?,?,?)");
for (int i = 1; i <= 10; i++) {
pstmt->setInt(1, i);
pstmt->setString(2, "12345");
pstmt->setString(3, "20110420104820");
pstmt->executeUpdate();
}
delete pstmt;
/* Select in ascending order */
pstmt = con->prepareStatement("SELECT id,ip FROM tests ORDER BY id ASC");
res = pstmt->executeQuery();
/* Fetch in reverse = descending order! */
res->afterLast();
while (res->previous())
{
cout << "\t... MySQL counts: " << res->getInt("id") << endl;
cout << "\t... MySQL counts: " << res->getString("ip") << endl;
}
delete res;
delete pstmt;
delete con;
} catch (sql::SQLException &e) {
cout << "End"<< endl;
}
执行到pstmt->executeUpdate();
这一句是,程序抛出异常,而且保存的内容也为0;
研究了很久也不知道是什么原因。
想请教各位大侠怎么样才能正确保存数据呢?
好吧 我知道原因了,一个dll文件没有链接上去。麻烦各位了 展开
2011-04-20
展开全部
ager.getConnection("server=.;database=数据库名:uid=sa;pwd=");
我们就用这个,你可以试一下。。
另外,站长团上有产品团购,便宜有保证
我们就用这个,你可以试一下。。
另外,站长团上有产品团购,便宜有保证
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询