pyodbc/sql-新创建的表将转到主数据库而不是指定的数据库

htrmnn0y  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(408)

我正在为类开发一个项目,在这个项目中,我使用python在我的2016 sql server中创建一个数据库和表。我已经安装了pyodbc,并且能够在python和我的sql服务器之间建立连接。但是,当我运行代码时,我可以创建“mydatabase”数据库…但是我要创建的后续表将转到“master”数据库中的“tables”文件夹。
我尝试过在同一个python文件中创建数据库和表,以及只添加表。结果总是一样的-它进入主数据库,而不是“mydatabase”数据库。我在google上搜索了不少,似乎我的代码写得很正确;所以,我不知道现在该看什么。
下面是一个创建数据库和表的代码在一个公平的猛扑。。。

import pyodbc

mydb = pyodbc.connect('Driver={SQL Server};'
                      'Server=DESKTOP-5I015MM\SQLEXPRESS;'
                      'Trusted_Connection=yes;')

mydb.autocommit = True

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

Q1 = "CREATE TABLE Contact(id INT IDENTITY(1,1) PRIMARY KEY, \
        username varchar(255) NOT NULL, password varchar(255) NOT NULL, \
        firstName varchar(255) NOT NULL, middleName varchar(255) NOT NULL, \
        lastName varchar(255) NOT NULL, address varchar(255) NOT NULL, \
        officePhone varchar(15) NOT NULL, cellPhone varchar(15) NOT NULL, \
        email varchar(255) NOT NULL)"
mycursor.execute(Q1)

Q2 = "CREATE TABLE Manufacturer(name varchar(255) PRIMARY KEY, \
        registeredCountry varchar(255) NOT NULL, \
        contactPerson INT NOT NULL, \
        FOREIGN KEY(contactPerson) REFERENCES Contact(id))"
mycursor.execute(Q2)

Q3 = "CREATE TABLE TestLab(name varchar(255) PRIMARY KEY, \
        address varchar(255) NOT NULL, \
        contactPerson INT NOT NULL, \
        FOREIGN KEY(contactPerson) REFERENCES Contact(id))"
mycursor.execute(Q3)

Q4 = "CREATE TABLE Product(modelNumber varchar(255) PRIMARY KEY, \
        manufacturer varchar(255) NOT NULL, \
        maufacturingDate date NOT NULL, \
        prodLength decimal(5,2) NOT NULL, \
        prodWidth decimal(5,2) NOT NULL, \
        cellTechnology varchar(255) NOT NULL, \
        cableType varchar(255) NOT NULL, \
        maximumSystemVoltage varchar(255) NOT NULL, \
        ratedVoc decimal(3,1) NOT NULL, \
        ratedIsc decimal(3,2) NOT NULL, \
        ratedVmp decimal(3,1) NOT NULL, \
        ratedImp decimal(3,2) NOT NULL, \
        ratedPmp decimal(4,1) NOT NULL, \
        ratedFf decimal(3,1) NOT NULL, \
        FOREIGN KEY(manufacturer) REFERENCES Manufacturer(name))"
mycursor.execute(Q4)

Q5 = "CREATE TABLE TestResults(dataSource varchar(255) NOT NULL, \
        modelNumber varchar(255) NOT NULL, \
        reportingCondition varchar(255) NOT NULL, \
        testSequence varchar(255) NOT NULL, \
        testDate date NOT NULL, \
        isc decimal(3,2) NOT NULL, \
        voc decimal(3,1) NOT NULL, \
        imp decimal(3,2) NOT NULL, \
        vmp decimal(3,1) NOT NULL, \
        pmp decimal(4,1) NOT NULL, \
        ff decimal(3,1) NOT NULL, \
        noct decimal(3,1) NOT NULL, \
        PRIMARY KEY(dataSource, modelNumber), \
        FOREIGN KEY(dataSource) REFERENCES TestLab(name), \
        FOREIGN KEY(modelNumber) REFERENCES Product(modelNumber))"
mycursor.execute(Q5)

如果数据库已经存在于sql中,下面是在“mydatabase”数据库中创建表的代码。。。

import pyodbc

mydb = pyodbc.connect('Driver={SQL Server};'
                      'Server=DESKTOP-5I015MM\SQLEXPRESS;'
                      'Database=mydatabase;'
                      'Trusted_Connection=yes;')

mydb.autocommit = True

mycursor = mydb.cursor()

Q1 = "CREATE TABLE Contact(id INT IDENTITY(1,1) PRIMARY KEY, \
        username varchar(255) NOT NULL, password varchar(255) NOT NULL, \
        firstName varchar(255) NOT NULL, middleName varchar(255) NOT NULL, \
        lastName varchar(255) NOT NULL, address varchar(255) NOT NULL, \
        officePhone varchar(15) NOT NULL, cellPhone varchar(15) NOT NULL, \
        email varchar(255) NOT NULL)"
mycursor.execute(Q1)

Q2 = "CREATE TABLE Manufacturer(name varchar(255) PRIMARY KEY, \
        registeredCountry varchar(255) NOT NULL, \
        contactPerson INT NOT NULL, \
        FOREIGN KEY(contactPerson) REFERENCES Contact(id))"
mycursor.execute(Q2)

Q3 = "CREATE TABLE TestLab(name varchar(255) PRIMARY KEY, \
        address varchar(255) NOT NULL, \
        contactPerson INT NOT NULL, \
        FOREIGN KEY(contactPerson) REFERENCES Contact(id))"
mycursor.execute(Q3)

Q4 = "CREATE TABLE Product(modelNumber varchar(255) PRIMARY KEY, \
        manufacturer varchar(255) NOT NULL, \
        maufacturingDate date NOT NULL, \
        prodLength decimal(5,2) NOT NULL, \
        prodWidth decimal(5,2) NOT NULL, \
        cellTechnology varchar(255) NOT NULL, \
        cableType varchar(255) NOT NULL, \
        maximumSystemVoltage varchar(255) NOT NULL, \
        ratedVoc decimal(3,1) NOT NULL, \
        ratedIsc decimal(3,2) NOT NULL, \
        ratedVmp decimal(3,1) NOT NULL, \
        ratedImp decimal(3,2) NOT NULL, \
        ratedPmp decimal(4,1) NOT NULL, \
        ratedFf decimal(3,1) NOT NULL, \
        FOREIGN KEY(manufacturer) REFERENCES Manufacturer(name))"
mycursor.execute(Q4)

Q5 = "CREATE TABLE TestResults(dataSource varchar(255) NOT NULL, \
        modelNumber varchar(255) NOT NULL, \
        reportingCondition varchar(255) NOT NULL, \
        testSequence varchar(255) NOT NULL, \
        testDate date NOT NULL, \
        isc decimal(3,2) NOT NULL, \
        voc decimal(3,1) NOT NULL, \
        imp decimal(3,2) NOT NULL, \
        vmp decimal(3,1) NOT NULL, \
        pmp decimal(4,1) NOT NULL, \
        ff decimal(3,1) NOT NULL, \
        noct decimal(3,1) NOT NULL, \
        PRIMARY KEY(dataSource, modelNumber), \
        FOREIGN KEY(dataSource) REFERENCES TestLab(name), \
        FOREIGN KEY(modelNumber) REFERENCES Product(modelNumber))"
mycursor.execute(Q5)
x9ybnkn6

x9ybnkn61#

在那一排之后 Mycursor.execute("CREATE DATABASE mydatabase") 添加 mycursor.execute("USE mydatabase")

相关问题