听说论坛挂了,不知道还能发不?
我写一个电子书管理程序,用MySQL做数据库。数据库名称为libmanager,其中有三个表:mysql> show tables;
+----------------------+
| Tables_in_libmanager |
+----------------------+
| author |
| book |
| type |
+----------------------+
3 rows in set (0.32 sec)三个表的描述:mysql> describe book;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| book_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| book_name | varchar(100) | NO | | NULL | |
| book_page | int(11) | NO | | NULL | |
| book_type_id | int(10) unsigned | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
4 rows in set (0.27 sec)mysql> describe author;
+-----------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| book_id | int(10) unsigned | NO | PRI | NULL | |
| book_author | varchar(100) | NO | | NULL | |
| book_translater | varchar(100) | YES | | NULL | |
+-----------------+------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)mysql> describe type;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| book_type_id | int(10) | NO | | 0 | |
| book_type | varchar(100) | NO | PRI | NULL | |
+--------------+--------------+------+-----+---------+-------+
2 rows in set (0.05 sec)
程序中有一项功能是用户输入书名(book.book_name),然后就将两个表(type表不删,因为同一个类型里可能有多本书)中相应那行数据删除,程序源代码如下://删除数据
int DeleteDatabase(MYSQL ** connect, string & questr)
{
if (mysql_query(*connect, questr.c_str()))
{
return 1;
}
return 0;
}//删除一本书
void DeleteOne(MYSQL ** connect)
{
//只提供按书名删除了吧,另外type表就不删了
string bookName;
cout<<"输入要删除的书名:";
cin>>bookName;
string que = "delete from book,author using book,author where book.book_name = \"";
que += bookName;
que += "\" AND book.book_id = author.book_id";
cout<<que.c_str()<<endl;
cin.get();
if (DeleteDatabase(connect, bookName))
{
cout<<"删除数据失败!"<<endl
<<"错误代码为:"<<mysql_error(*connect)<<endl;
cin.get();
cin.get();
}
else
{
cout<<"删除数据成功"<<endl;
}
}我有意把程序生成的查询语句输出到shell里了。
程序运行时出错,比如现在我想删除《娱乐至死》这本书,先在MySQL命令行里检查mysql> select * from book where book_name = "娱乐至死";
+---------+--------------+-----------+--------------+
| book_id | book_name | book_page | book_type_id |
+---------+--------------+-----------+--------------+
| 924 | 娱乐至死 | 237 | 402 |
+---------+--------------+-----------+--------------+
1 row in set (0.08 sec)说明是有这本书的,接着运行我的程序进行删除,结果出错:输入要删除的书名:娱乐至死
delete from book,author using book,author where book.book_name = "娱乐至死" AND book.book_id = author.book_id
删除数据失败!
错误代码为:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '娱乐至死' at line 1现在我把输出的查询语句,也就是"delete from book,author using book,author where book.book_name = "娱乐至死" AND book.book_id = author.book_id"完整的复制到MySQL的命令行里,就是在最后再加个分号,然后回车执行,成功了,如下:mysql> delete from book,author using book,author where book.book_name = "娱乐至死" AND book.book_id = author.book_id;
Query OK, 2 rows affected (0.28 sec)mysql> select * from book where book_name = "娱乐至死";
Empty set (0.02 sec)可以看到书目信息已经成功删除了。为啥我用C语言调用库函数执行的删除操作不成功,在MySQL命令行里执行却成功了?我的程序还有插入数据,删除所有数据,查找数据等操作,均成功。
开发环境:Ubuntu10.04 linux2.6.38 gcc 4.4.3 mysql 5.1.41 谢谢各位!
我写一个电子书管理程序,用MySQL做数据库。数据库名称为libmanager,其中有三个表:mysql> show tables;
+----------------------+
| Tables_in_libmanager |
+----------------------+
| author |
| book |
| type |
+----------------------+
3 rows in set (0.32 sec)三个表的描述:mysql> describe book;
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| book_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| book_name | varchar(100) | NO | | NULL | |
| book_page | int(11) | NO | | NULL | |
| book_type_id | int(10) unsigned | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
4 rows in set (0.27 sec)mysql> describe author;
+-----------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| book_id | int(10) unsigned | NO | PRI | NULL | |
| book_author | varchar(100) | NO | | NULL | |
| book_translater | varchar(100) | YES | | NULL | |
+-----------------+------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)mysql> describe type;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| book_type_id | int(10) | NO | | 0 | |
| book_type | varchar(100) | NO | PRI | NULL | |
+--------------+--------------+------+-----+---------+-------+
2 rows in set (0.05 sec)
程序中有一项功能是用户输入书名(book.book_name),然后就将两个表(type表不删,因为同一个类型里可能有多本书)中相应那行数据删除,程序源代码如下://删除数据
int DeleteDatabase(MYSQL ** connect, string & questr)
{
if (mysql_query(*connect, questr.c_str()))
{
return 1;
}
return 0;
}//删除一本书
void DeleteOne(MYSQL ** connect)
{
//只提供按书名删除了吧,另外type表就不删了
string bookName;
cout<<"输入要删除的书名:";
cin>>bookName;
string que = "delete from book,author using book,author where book.book_name = \"";
que += bookName;
que += "\" AND book.book_id = author.book_id";
cout<<que.c_str()<<endl;
cin.get();
if (DeleteDatabase(connect, bookName))
{
cout<<"删除数据失败!"<<endl
<<"错误代码为:"<<mysql_error(*connect)<<endl;
cin.get();
cin.get();
}
else
{
cout<<"删除数据成功"<<endl;
}
}我有意把程序生成的查询语句输出到shell里了。
程序运行时出错,比如现在我想删除《娱乐至死》这本书,先在MySQL命令行里检查mysql> select * from book where book_name = "娱乐至死";
+---------+--------------+-----------+--------------+
| book_id | book_name | book_page | book_type_id |
+---------+--------------+-----------+--------------+
| 924 | 娱乐至死 | 237 | 402 |
+---------+--------------+-----------+--------------+
1 row in set (0.08 sec)说明是有这本书的,接着运行我的程序进行删除,结果出错:输入要删除的书名:娱乐至死
delete from book,author using book,author where book.book_name = "娱乐至死" AND book.book_id = author.book_id
删除数据失败!
错误代码为:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '娱乐至死' at line 1现在我把输出的查询语句,也就是"delete from book,author using book,author where book.book_name = "娱乐至死" AND book.book_id = author.book_id"完整的复制到MySQL的命令行里,就是在最后再加个分号,然后回车执行,成功了,如下:mysql> delete from book,author using book,author where book.book_name = "娱乐至死" AND book.book_id = author.book_id;
Query OK, 2 rows affected (0.28 sec)mysql> select * from book where book_name = "娱乐至死";
Empty set (0.02 sec)可以看到书目信息已经成功删除了。为啥我用C语言调用库函数执行的删除操作不成功,在MySQL命令行里执行却成功了?我的程序还有插入数据,删除所有数据,查找数据等操作,均成功。
开发环境:Ubuntu10.04 linux2.6.38 gcc 4.4.3 mysql 5.1.41 谢谢各位!
结果就是:
delete from book,author using book,author where book.book_name = "娱乐至死" AND book.book_id = author.book_id
{
cout<<"删除数据失败!"<<endl
<<"错误代码为:"<<mysql_error(*connect)<<endl;
cin.get();
cin.get()
}谢谢帮助,我结贴了!