简体   繁体   English

无法使用sqlite3 Python模块正确执行SQL脚本

[英]Cannot properly execute a sql script with sqlite3 Python module

I'm trying to execute SQL scripts contained in a .sql file with the sqlite3 Python module, in order to migrate the database from a Drupal site to a Wordpress one. 我正在尝试使用sqlite3 Python模块执行.sql文件中包含的SQL脚本,以便将数据库从Drupal站点迁移到Wordpress站点。

So I open and read the .sql thing with this little script: 因此,我使用以下小脚本打开并阅读了.sql内容:

def executeSQLScriptsFromFile(filename):
    fd = open(filename, 'r')
    sqlFile = fd.read()
    fd.close()

    sqlCommands = sqlFile.split(';')

    for command in sqlCommands:
        print " === BEGIN \n" + command + "\nEND === \n"
        try:
            c.execute(command)
        except OperationalError, msg:
            print "Command skipped: ", msg 

And when I execute this code with SQL commands I write, everything works fine: CREATE TABLE, SELECT, DELETE... 当我使用编写的SQL命令执行此代码时,一切工作正常:CREATE TABLE,SELECT,DELETE ...

But when I try with the SQL script that I downloaded from a website database I work on: 但是,当我尝试从网站数据库下载的SQL脚本时,我的工作是:

CREATE TABLE IF NOT EXISTS `node` (
  `nid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `vid` int(10) unsigned NOT NULL DEFAULT '0',
  `type` varchar(32) NOT NULL DEFAULT '', 
  `language` varchar(12) NOT NULL DEFAULT '', 
  `title` varchar(255) NOT NULL DEFAULT '', 
  `uid` int(11) NOT NULL DEFAULT '0',
  `status` int(11) NOT NULL DEFAULT '1',
  `created` int(11) NOT NULL DEFAULT '0',
  `changed` int(11) NOT NULL DEFAULT '0',
  `comment` int(11) NOT NULL DEFAULT '0',
  `promote` int(11) NOT NULL DEFAULT '0',
  `moderate` int(11) NOT NULL DEFAULT '0',
  `sticky` int(11) NOT NULL DEFAULT '0',
  `tnid` int(10) unsigned NOT NULL DEFAULT '0',
  `translate` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`nid`),
  UNIQUE KEY `vid` (`vid`),
  KEY `node_changed` (`changed`),
  KEY `node_created` (`created`),
  KEY `node_moderate` (`moderate`),
  KEY `node_promote_status` (`promote`,`status`),
  KEY `node_status_type` (`status`,`type`,`nid`),
  KEY `node_title_type` (`title`,`type`(4)),
  KEY `node_type` (`type`(4)),
  KEY `uid` (`uid`),
  KEY `tnid` (`tnid`),
  KEY `translate` (`translate`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=663 ;

I got this exception: 我有这个例外:

Command skipped:  near "unsigned": syntax error

As you can see, I am not fluent in SQL :p 如您所见,我不太懂SQL:p

Does someone know what may be the problem ? 有人知道可能是什么问题吗?

sqlite3 does not support "unsigned" and it does not support AUTO_INCREMENT. sqlite3不支持“无符号”,并且不支持AUTO_INCREMENT。 there are probably some other directive that sqlite3 does not support ... I think that you got that file from someone who was targeting postgres or mysql... to fix it remove all directives that are not supported by sqlite3, or use the same database engine that file targets 可能还有其他一些sqlite3不支持的指令...我认为您是从针对postgres或mysql的人那里获得的文件...要修复该文件,请删除sqlite3不支持的所有指令,或者使用同一数据库该文件目标的引擎

as an aside you can do 顺便说一句,你可以做

 c.executescript(open("my_sqcript.sql").read())

here is the schema that will work for sqlite3 这是适用于sqlite3的架构

CREATE TABLE IF NOT EXISTS `node` (
  `nid` int(10) NOT NULL,
  `vid` int(10) NOT NULL DEFAULT '0',
  `type` varchar(32) NOT NULL DEFAULT '', 
  `language` varchar(12) NOT NULL DEFAULT '', 
  `title` varchar(255) NOT NULL DEFAULT '', 
  `uid` int(11) NOT NULL DEFAULT '0',
  `status` int(11) NOT NULL DEFAULT '1',
  `created` int(11) NOT NULL DEFAULT '0',
  `changed` int(11) NOT NULL DEFAULT '0',
  `comment` int(11) NOT NULL DEFAULT '0',
  `promote` int(11) NOT NULL DEFAULT '0',
  `moderate` int(11) NOT NULL DEFAULT '0',
  `sticky` int(11) NOT NULL DEFAULT '0',
  `tnid` int(10)  NOT NULL DEFAULT '0',
  `translate` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`nid`),
  UNIQUE(`vid`)

)   ;

here is the table as defined by sqlalchemy 这是sqlalchemy定义的表

class Node(Base):
   __tablename__ = 'node'
   nid     = Column(Integer,primary_key=True)
   vid     = Column(Integer,default=0)
   type    = Column(String,default="")
   language= Column(String,default="")
   title   = Column(String,default="")
   uid     = Column(Integer,default=0)
   status  = Column(Integer,default=1)
   created = Column(Integer,default=0)
   changed = Column(Integer,default=0)
   comment = Column(Integer,default=0)
   promote = Column(Integer,default=0)
   moderate= Column(Integer,default=0) 
   sticky  = Column(Integer,default=0)
   tnid    = Column(Integer,default=0)
   translate= Column(Integer,default=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM