oracle python操作 增刪改查

2022-06-19 23:09:16 字數 3069 閱讀 6459

oracle刪除

刪除表內容 

truncate table new_userinfo;

刪除表drop table new_userinfo;

1.首先,python鏈結oracle資料庫需要配置好環境。

1)python:python 3.6.3 (v3.6.3:2c5fed8, oct 3 2017, 17:26:49) [msc v.1900 32 bit (intel)] on win32

python\python36-32\lib\site-packages資料夾中。

3.在python中輸入:

1import cx_oracle

沒有報錯的話說明驅動安裝成功。

4.資料庫連線操作:12

3456

7cur = conn.cursor()

sql = "select * from dual"

cur.execute(sql)

cur.close()

conn.commit()

conn.close()

5.資料庫查詢:12

3456

78910

1112

1314

1516

1718

1920

2122

23import cx_oracle

conn = cx_oracle.connect('xzt/xzt@localhost/testdb')

cursor = conn.cursor ()

cursor.execute ("select * from student_tb")

rows = cursor.fetchall() #得到所有資料集

for row in rows:

print("%d, %s, %s, %s" % (row[0], row[1], row[2], row[3]))#python3以上版本中print()要加括號用了

print("number of rows returned: %d" % cursor.rowcount)

cursor.execute ("select * from student_tb")

while (true):

row = cursor.fetchone() #逐行得到資料集

if row == none:

break

print("%d, %s, %s, %s" % (row[0], row[1], row[2], row[3]))

print("number of rows returned: %d" % cursor.rowcount)

cursor.close ()

conn.close ()

6.資料庫插入:12

3456

78910

1112

1314

import cx_oracle

conn = cx_oracle.connect('xzp/xzp@localhost/testdb')

cursor = conn.cursor()

cursor.execute ("create table inserttest(id int, c1 varchar(50), c2 varchar(50), c3 varchar(50))")

cursor.execute ("insert into inserttest (id, col1, col2, col3)values(1213412, 'asdfa', 'ewewe', 'sfjgsfg')")

cursor.execute ("insert into inserttest (id, col1, col2, col3)values(12341, 'ashdfh', 'shhsdfh', 'sghs')")

cursor.execute ("insert into inserttest (id, col1, col2, col3)values(123451235, 'werwerw', 'asdfaf', 'awew')")

conn.commit() #這裡一定要commit才行,要不然資料是不會插入的

cursor.close()

conn.close()

上面的**用了遞迴運算,迴圈讀取彩票資訊,可以一直讀取到2023年第一期,由於使用的是遞迴,在效能上不是很好,**非常吃記憶體,電腦記憶體不夠的朋友就不要嘗試了,容易宕機。

參考:

ORACLE PYTHON實戰 複製A表資料到B表

最近在學習python 看到了pythod的oracle,不僅可以一次fetch多條,也可以一次insert多條,想寫乙個複製a表資料到b表的程式來看看實際效率能不能提高。寫完發現,非常驚豔!效率提公升了近一倍!當然可能會認為這個沒有實際意義,其實不然。從a表複製資料到b表有很多中方法,一般直接in...

檔案操作 增改查

修改檔案的兩種方式 1.第一種方法 在原檔案中修改 先開啟檔案,把檔案內容讀出來賦值給乙個變數,關閉檔案,重新開啟檔案,把檔案內容寫到檔案中 with open r f r encoding utf 8 as f data f.read print data print type data with...

iOS 對plist增改操作

這篇文章是自己通過實踐獲取,在網上查過很多資料,也走了不上的彎路,由於剛開始學子不久,只是把自己遇到的問題貢獻給大家 一,建立檔案 獲取路徑物件 nsarray paths nssearchpathfordirectoriesindomains nsdocumentdirectory,nsuserd...