开发者

Create a function in Python which creates a mySQL database

开发者 https://www.devze.com 2023-01-21 10:22 出处:网络
I\'ve created a program in python and mySQL which creates a database and imports the data from text files and puts them into 4 different columns. The code works but I want to change the code and creat

I've created a program in python and mySQL which creates a database and imports the data from text files and puts them into 4 different columns. The code works but I want to change the code and create functions.

Can anyone please help me create a function that creates a mySQL database? Here is the code I have at the moment. Thanks in advance!

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 'activi开发者_Go百科ties.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()

Okay, this is what I have so far.

I'm getting this error: TypeError: data_entry() takes no arguments (1 given)

And, the data also isn't being inserted into the table from the text files. Any help would be greatly appreciated!!

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()


Your sql for loading data should be

sql = '''LOAD DATA LOCAL INFILE %s INTO TABLE popularity 
         FIELDS TERMINATED BY ';' 
         LINES TERMINATED BY '\\n' 
         (PersonNumber, Value, Category) 
         SET Category=%s
     '''

+ %s --> '%s'
+ remove " at the end

It should work. Besides, load_files --> load_file

0

精彩评论

暂无评论...
验证码 换一张
取 消