Mysql创建表如果不存在其他....信息架构

时间:2017-04-14 23:42:15

标签: php mysql

快点一个希望 - 不知道我在哪里出错但这似乎没有完全停止。

通过PHP运行MySQL查询...

当前代码

$uu = mysql_query("
IF EXISTS(SELECT table_name FROM information_schema.tables 
WHERE table_schema = 'joelvvkf_c' AND table_name = 'test_q')
THEN
insert into `test_q` (code, va_desc, price, category) 
values ('$code', '$desc', '$price', '$categ') 
on duplicate key update va_desc='$desc', price='$price', category='$categ'
ELSE
CREATE TABLE `test_quote` (
`code` varchar(30) NOT NULL,
`va_desc` text NOT NULL,
`price` text NOT NULL,
`category` text NOT NULL,
PRIMARY KEY (`code`),
UNIQUE KEY `id` (`code`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
insert into `test_q` (code, va_desc, price, category) 
values ('$code', '$desc', '$price', '$categ')
END IF;
")or die(mysql_error());

真的很欣赏我需要改变的一些帮助,目前这绝对没有任何帮助,并且不会返回任何特定的错误。 :/

话虽如此,如果我在phpMyAdmin中运行它会返回以下内容(虽然我无法理解为什么):

#1064 - You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server 
version for the right syntax to use near 
'ELSE CREATE TABLE `test_quote`(
`code` varchar(30) NOT NULL,
`va_desc` text NO' at line 7 

2 个答案:

答案 0 :(得分:5)

您无需查询print(''.join(str(upBound if x =='x' else x) for x in code) 。您可以使用INFORMATION_SCHEMA选项IF NOT EXISTS

CREATE TABLE

此外,主键是唯一键,在创建表时无需同时指定它们。

答案 1 :(得分:0)

尝试使用以下查询,就像在sql查询中不支持else,then语句一样,因为您可以存储过程。

dat <- data.frame(checknum,check, stringsAsFactors=FALSE)
相关问题