Python sqlite3操作E

2024-06-16 08:35:52 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码。它将以相同的格式读取一堆带有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()

错误:

^{pr2}$

为了避免混乱,这里是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:
'),

Tags: keycidefaultstringlinenotutf8null