日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python在sql添加数据库_使用Python创建MySQL数据库实现字段动态增加以及动态的插入数据...

發布時間:2024/10/8 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python在sql添加数据库_使用Python创建MySQL数据库实现字段动态增加以及动态的插入数据... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

應用場景:

我們需要設計一個數據庫來保存多個文檔中每個文檔的關鍵字。假如我們每個文檔字符都超過了1000,取其中出現頻率最大的為我們的關鍵字。

假設每個文檔的關鍵字都超過了300,每一個文件的0-299號存儲的是我們的關鍵字。那我們要建這樣一個數據庫,手動輸入這樣的一個表是不現實的,我們只有通過程序來幫我實現這個重復枯燥的操作。

具體的示意圖如下所示:

首先圖1是我們的原始表格:

圖1

這個時候我們需要程序來幫我們完成自動字段的創建和數據的插入。

圖2

上圖是我們整個表的概況。下面我們就用程序來總結出這樣的一個表格是怎么實現的。

'''

function description : Add the fields and data dynamicly.

data : 2014-08-04

author : Chicho

running : python addfileds.py

'''

import MySQLdb

#connect the database

#the argvs based on the database you set.

#Generally speaking, you should change the No. of the port 3306 , because it's easy to be attack

#localhost = 127.0.0.1

conn = MySQLdb.connect(host = 'localhost', port = 3306, user = 'root', passwd = '*****')

curs = conn.cursor()

# create a database named addtest

#Ensure the program can run multiple times,we should use try...exception

try:

curs.execute('create database addtest')

except:

print 'Database addtest exists!'

conn.select_db('addtest')

# create a table named addfields

try:

curs.execute('create table addfields(id int PRIMARY KEY NOT NULL,name text)')

except:

print('The table addfields exists!')

# add the fileds

try:

for i in range(1):

sql = "alter table addfields add key%s text" %i

curs.execute(sql)

except Exception,e:

print e

for i in range(4): #insert 5 lines

sql = "insert into addfields set id=%s" %i

curs.execute(sql)

sql = "update addfields set name = 'hello%s' where id= %s"%(i,i)

curs.execute(sql)

for j in range(5):

sql = "update addfields set key%s = 'world%s%s' where id=%s"%(j,i,j,i)

curs.execute(sql)

#this is very important

conn.commit()

curs.close()

conn.close()

記住最后一定要記得最后三行這個語句,否則你的操作不會寫入到數據庫中。

最后就可以得到我們的結果,如下圖所示:

程序的大體實現就是這樣。

參考文獻:

感謝樓上幾位博主的無私奉獻精神,博主是在沒有MySQL 的基礎上參照這些blog實現的。如有什么地方不足歡迎提出

批評和建議。對你的意見我在此表示由衷的感謝。

彩蛋:

1.操作數據庫出現的一些錯誤總結

如果你長時間為隊數據庫進行操作,再次進行操作的時候可能會出現以下錯誤:

raise errorclass, errorvalue

OperationalError: (2006, ‘MySQL server has gone away‘)

這個時候對于MySQL server 你要做的就是執行一下下面這個命令

connect your_database

對于在python中的IDLE你需要執行:

conn = MySQLdb.connect(host = 'localhost', port = 3306, user = 'root', passwd = '****')

curs = conn.cursor()

conn.select_db('addtest')密碼輸入你自己數據庫中設置的。

2UnicodeEncodeError: ‘latin-1‘ codec can‘t encode characters in position

出現上述這個錯誤的時候可以采用下面這個方法就可以解決。

conn.set_character_set('utf8')

curs.execute('set names utf8')

curs.execute('SET CHARACTER SET utf8;')

curs.execute('SET character_set_connection=utf8;')conn,curs和本文中參數設置是一樣的。

原文:http://blog.csdn.net/chichoxian/article/details/38224361

總結

以上是生活随笔為你收集整理的python在sql添加数据库_使用Python创建MySQL数据库实现字段动态增加以及动态的插入数据...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。