实际上,不可能准备所有SQL语句,因为MySQL仅允许以下类型的SQL语句可以准备:
mysql> PREPARE stmt FROM 'SELECT tender_value from Tender WHERE Companyname = ?'; Statement prepared mysql> SET @A = 'Singla Group.'; mysql> EXECUTE stmt using @A; +--------------+ | tender_value | +--------------+ | 220.255997 | +--------------+ 1 row in set (0.07 sec) mysql> DEALLOCATE PREPARE stmt;
修改数据的语句。
mysql> PREPARE stmt1 FROM 'DELETE from Tender WHERE Sr = ?'; Statement prepared mysql> SET @A = 4; mysql> EXECUTE stmt1; ERROR 1210 (HY000): Unknown error 1210 mysql> EXECUTE stmt1 using @A; mysql> DEALLOCATE PREPARE stmt1; mysql> Select * from tender; +----+---------------+--------------+ | Sr | CompanyName | Tender_value | +----+---------------+--------------+ | 1 | Abc Corp. | 250.369003 | | 2 | Khaitan Corp. | 265.588989 | | 3 | Singla group. | 220.255997 | +----+---------------+--------------+ 3 rows in set (0.00 sec)
mysql> PREPARE stmt3 FROM 'CREATE TABLE Student(Id INT, Name Varchar(20))'; Statement prepared mysql> EXECUTE stmt3; mysql> DEALLOCATE PREPARE stmt3;
mysql> PREPARE stmt10 FROM 'SHOW TABLES'; Statement prepared mysql> EXECUTE stmt10; +-------------------+ | Tables_in_query | +-------------------+ | emp | | emp123 | | emp_t | | examination_btech | | new_number | | student | | student_detail | | student_info | | tender | | website | +-------------------+ 10 rows in set (0.00 sec)