# file: load.py
# 把文件中的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫中
# 輸入?yún)?shù):導(dǎo)入到哪張表,導(dǎo)入哪個(gè)文件
from MySQLdb import *
# 連接數(shù)據(jù)庫
db = connect(host = 'localhost',
user = 'root',
passwd = 'colorfulgreen',
db = 'test')
# 獲取數(shù)據(jù)庫游標(biāo)
cur = db.cursor()
# 獲取當(dāng)前使用的數(shù)據(jù)庫名字
database = cur.execute('select database()') # 執(zhí)行語句
database = cur.fetchall() # 從游標(biāo)中讀取數(shù)據(jù)
print 'the current database: %s' %(database[0]) # 輸出
# 列出數(shù)據(jù)庫中的所有表
show_tables = cur.execute('show tables')
show_tables = cur.fetchall()
print 'all tables: '
for tmp_line in show_tables:
print tmp_line[0]
# 選擇表
load_to_table = raw_input('please select the table to load to:')
# print 'the data will be load to %s' %load_to_table
# 輸入要導(dǎo)入的文件
load_from_file = raw_input('plese input the file path to load from:')
# print 'the data will be load from %s' %load_from_file
# 導(dǎo)入
sql = 'load data local infile \'' + load_from_file + '\' into table ' + load_to_table # 注意文件名兩邊要有引號!!!
load_num = cur.execute(sql)
print '%d records have been loaded' %load_num