python创建数据库代码_用Python创建一个函数,创建一个mySQL数据库?

我用python和mySQL创建了一个程序,它创建一个数据库并从文本文件中导入数据并将它们放到4个不同的列中。代码有效,但我想更改代码并创建函数。

如何创建一个函数,创建一个mySQL数据库?这是我目前的代码。import MySQLdb

# Create connection to the MySQL database - Make sure host, user,

# passwd are consistent with the database you are trying to conect to

def create_database():

db_connection = MySQLdb.connect(host='localhost', user='root', passwd='password')

# Variable that exacutes Database calls with MySQL

cursor = db_connection.cursor()

# Create databse with MYSQL query - databasename

cursor.execute('CREATE DATABASE inb104')

# Select which database to use with MYSQL query - databasename

cursor.execute('USE inb104')

# Create database with MYSQL query - tablename & fields

cursor.execute('''CREATE TABLE popularity (

PersonNumber INT,

Value VARCHAR(70),

Category VARCHAR(25),

PRIMARY KEY (PersonNumber, Value, Category)

)

''')

cursor.execute("LOAD DATA LOCAL INFILE 'tv.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='TV'")

cursor.execute("LOAD DATA LOCAL INFILE 'actors.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='Actors'")

cursor.execute("LOAD DATA LOCAL INFILE 'movies.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='Movies'")

cursor.execute("LOAD DATA LOCAL INFILE 'sports.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='Sports'")

cursor.execute("LOAD DATA LOCAL INFILE 'activities.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='Activities'")

cursor.execute("LOAD DATA LOCAL INFILE 'musicians.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='Musicians'")

cursor.execute("LOAD DATA LOCAL INFILE 'games.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='Games'")

cursor.execute("LOAD DATA LOCAL INFILE 'books.txt' INTO TABLE popularity FIELDS TERMINATED BY '\\t' LINES TERMINATED BY '\\n' (PersonNumber, Value, Category) SET Category='Books'")

# commit the changes to the database

db_connection.commit()

# close the cursor and connection

cursor.close()

db_connection.close()

得到error:TypeError: data_entry() takes no arguments (1 given)

而且,数据也没有从文本文件插入到表格中。import MySQLdb

def connect_to_database(user, password):

return MySQLdb.connect(host='localhost', user=user, passwd=password)

def create_database(cursor):

cursor.execute('CREATE DATABASE inb104')

cursor.execute('USE inb104')

cursor.execute('''CREATE TABLE popularity (

PersonNumber INT,

Value VARCHAR(70),

Category VARCHAR(25),

PRIMARY KEY (PersonNumber, Value, Category)

)

''')

def load_file(cursor, *files):

"""Load the files given in (filename, category) format."""

sql = '''LOAD DATA LOCAL INFILE '%s' INTO TABLE popularity

FIELDS TERMINATED BY '\\t'

LINES TERMINATED BY '\\n'

(PersonNumber, Value, Category)

SET Category='%s'")

'''

for filename, category in files:

cursor.execute(sql, (filename, category))

def data_entry():

"""Connect to the DB server, create the DB and table and load the table with records

"""

db = connect_to_database('root', 'password')

cursor = db.cursor()

create_database(cursor)

load_files(cursor,('tv.txt', 'TV'), ('actors.txt', 'Actors'),

('movies.txt', 'Movies'))

db.commit()

cursor.close()

db.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值