Python sqlite3 - 操作错误

时间:2015-06-18 14:01:49

标签: python sqlite

这是我的代码。它会以相同的格式读取一堆带有SQL命令的文件(即以-开头的注释,以及一些空行,因此我的循环中有if条件)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv,sqlite3,os

conn = sqlite3.connect('db_all.db')
c = conn.cursor()
files = os.listdir('C:\\Users\\ghb\\Desktop\\database_schema')

for file in files:
    string = ''
    with open(file, 'rb') as read:
        for line in read.readlines():
            if line[0]!='-' and len(line)!=0: string = string + line.rstrip() #error also occurs if I skip .rstrip
        print string #for debugging purposes
        c.executescript(string)
        string=''

conn.close()

错误:

Traceback (most recent call last):
  File "C:/Users/ghb/Desktop/database_schema/database_schema.py", line 16, in <module>
    c.executescript(string)
sqlite3.OperationalError: near "SET": syntax error

由于害怕混乱,这里是string变量的输出(没有INSERT INTO数据,这是保密的):

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";




CREATE TABLE IF NOT EXISTS `career` (
  `carKey` int(11) NOT NULL AUTO_INCREMENT,
  `persID` bigint(10) unsigned zerofill NOT NULL,
  `persKey` int(6) unsigned NOT NULL,
  `wcKey` int(2) unsigned NOT NULL,
  `wtKey` int(2) unsigned DEFAULT NULL,
  `pwtKey` int(2) unsigned DEFAULT NULL,
  `dptId` bigint(10) unsigned NOT NULL,
  `dptNr` int(4) unsigned NOT NULL,
  `dptalias` varchar(10) COLLATE utf8_icelandic_ci NOT NULL,
  `class` enum('A','B') COLLATE utf8_icelandic_ci NOT NULL,
  `getfilm` enum('yes','no') COLLATE utf8_icelandic_ci NOT NULL DEFAULT 'yes',
  `finished` enum('true','false') COLLATE utf8_icelandic_ci NOT NULL DEFAULT 'false',
  `startDate` date NOT NULL,
  `endDate` date DEFAULT NULL,
  `regDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `user` tinyint(4) NOT NULL,
  `status` set('LÉST','BRFL','BRFD','BRNN') COLLATE utf8_icelandic_ci DEFAULT NULL,
  `descr` text COLLATE utf8_icelandic_ci,
  PRIMARY KEY (`carKey`),
  KEY `pwtKey` (`pwtKey`),
  KEY `wtKey` (`wtKey`),
  KEY `dptId` (`dptId`),
  KEY `user` (`user`),
  KEY `persID` (`persID`),
  KEY `persKey` (`persKey`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_icelandic_ci AUTO_INCREMENT=2686 ;

输入样本:

INSERT INTO `career` (`carKey`, `persID`, `persKey`, `wcKey`, `wtKey`, `pwtKey`, `dptId`, `dptNr`, `dptalias`, `class`, `getfilm`, `finished`, `startDate`, `endDate`, `regDate`, `user`, 
(5, 34536, 346, 22, 44, 34, 3454356, 33, 'asdasd', 'ASDASD', 'ASDSD', 'true', '1991-02-04', '2010-05-02', '2009-05-02 00:01:02', 1, NULL, 'HH:
'),

0 个答案:

没有答案