姐姐派的活T_T - 1
# file: load.py
# 把文件中的數據導入到數據庫中
# 輸入參數:導入到哪張表,導入哪個文件
from MySQLdb import *
# 連接數據庫
db = connect(host = 'localhost',
user = 'root',
passwd = 'colorfulgreen',
db = 'test')
# 獲取數據庫游標
cur = db.cursor()
# 獲取當前使用的數據庫名字
database = cur.execute('select database()') # 執行語句
database = cur.fetchall() # 從游標中讀取數據
print 'the current database: %s' %(database[0]) # 輸出
# 列出數據庫中的所有表
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
# 輸入要導入的文件
load_from_file = raw_input('plese input the file path to load from:')
# print 'the data will be load from %s' %load_from_file
# 導入
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
# 把文件中的數據導入到數據庫中
# 輸入參數:導入到哪張表,導入哪個文件
from MySQLdb import *
# 連接數據庫
db = connect(host = 'localhost',
user = 'root',
passwd = 'colorfulgreen',
db = 'test')
# 獲取數據庫游標
cur = db.cursor()
# 獲取當前使用的數據庫名字
database = cur.execute('select database()') # 執行語句
database = cur.fetchall() # 從游標中讀取數據
print 'the current database: %s' %(database[0]) # 輸出
# 列出數據庫中的所有表
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
# 輸入要導入的文件
load_from_file = raw_input('plese input the file path to load from:')
# print 'the data will be load from %s' %load_from_file
# 導入
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
posted on 2010-08-19 00:46 小默 閱讀(259) 評論(1) 編輯 收藏 引用 所屬分類: Database