问题描述
这里的场景,我上传一个txt文件的sql查询。每次成功上传,我都需要执行它的内容。
Here's the scenario, I am uploading a txt file of sql queries in it. And every successful upload, I need to execute its content as well.
好吧,我似乎无法执行一组查询。只有单行查询。
Well, I cant seem to execute set of queries. Just single line query.
我有query.txt:
I have query.txt:
USE `wifi_analyzer`;
CREATE TABLE 'test' (
'username' varchar(10) NOT NULL,
'password' varchar(10) NOT NULL
);
这是我如何在我的cakephp文件中执行。
And this is how i execute it in my cakephp file.
$fullFile = $file_path.$file_name;
$readFile = fopen($file_path.$file_name, 'r');
$contents = fread($readFile,filesize($fullFile));
$this->User->query($contents);
可以读取文件及其内容。但是,它显示一个错误:
The file can be read as well as its content. However, It displays an error:
SQL Error: 1064: 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 'CREATE TABLE 'test' (
'username' varchar(10) NOT NULL,
'password' varchar(10' at line 3 [CORE\cake\libs\model\datasources\dbo_source.php, line 673]
好吧,如果它只是一个单行查询,像这样:
Well, if it is just a single line query like this:
Select * from sometable;
它不会显示任何错误,但是这个单个查询:
it doesn't display any error. But this single query:
CREATE TABLE 'test' (
'username' varchar(10) NOT NULL,
'password' varchar(10) NOT NULL
);
它不会执行。
更新:
我读过这个:
但dunno如果这将有助于,因为我在cakephp
but dunno if this will help since i am doing it in cakephp
推荐答案
不要对表和字段名使用单引号。
Dont use single quotes for table and field names. Execute below query it will work
CREATE TABLE `test` (
`username` varchar( 10 ) NOT NULL ,
`password` varchar( 10 ) NOT NULL
);
这篇关于多个SQL查询未在Cakephp中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!