python資料庫操作 mssql和oracle

2021-10-18 10:24:41 字數 2066 閱讀 8200

先安裝mysql官方python程式。

#!/usr/bin/python

# -*- coding: utf-8 -*-

#pip install mysql-connector

import mysql.connector

# 開啟資料庫連線

mydb = mysql.connector.connect(

host=

"localhost"

, user=

"root"

, passwd=

"123321"

, database=

"test"

)# 使用cursor()方法獲取操作游標

mycursor = mydb.cursor(

)'''

#刪除語句 execute(sql)後,

sql = "delete from sites where name = 'stackoverflow'"

mycursor.execute(sql)

mydb.commit()

'''#批量插入

'''sql = "insert into sites (name, url) values (%s, %s)"

val = [

('google', ''),

('github', ''),

('taobao', ''),

('stackoverflow', '')]

mycursor.executemany(sql, val)

mydb.commit() # 資料表內容有更新,必須使用到該語句

'''mycursor.execute(

"select * from zongbiao")

myresult = mycursor.fetchall(

)# fetchall() 獲取所有記錄

for x in myresult:

print

(x)

# -*- coding: utf-8 -*-

#pip install cx_oracle

#用途:操作oracle資料庫demo

import cx_oracle

import sys

import os

user =

"myuser"

passwd =

"myuser"

listener =

'172.14.0.79:1521/orcl'

conn = cx_oracle.connect(user, passwd, listener)

print

(conn)

cursor = conn.cursor(

)sql =

"select * from fundasset t where custid <75030000030"

#一次取一條資料,row為元組資料

cursor.execute(sql)

while(1

):row = cursor.fetchone(

)if row ==

none

:break

print

(row)

print

("--------------------------------------"

)#一次取所有資料,rows為元組列表資料

cursor.execute(sql)

rows = cursor.fetchall(

)for row in rows:

print

(row)

#支援對資料庫的插入、更新和刪除操作。輸入操作sql,執行無返回。

defother_operation

(sql)

: cursor.execute(sql)

conn.commit(

)print

(sql)

cursor.close(

)conn.close(

)

MSSQ遠端 本地資料庫排序規則衝突處置

2臺mssql資料庫伺服器通過鏈結伺服器進行資料互動。2臺資料庫伺服器之間的編碼規則是不一樣的。在進行資料匹配的時候,報 無法解決 equal to 運算中 chinese prc ci as 和 chinese prc bin 之間的排序規則衝突 檢查鏈結伺服器設定 將排序規則相容設定為 否 使用...

python 資料庫操作

例子1 建立乙個資料庫 coding utf 8 中文注釋 import mysqldb 建立和資料庫系統的連線 conn mysqldb.connect host localhost user root passwd 獲取操作游標 cursor conn.cursor 執行sql,建立乙個資料庫 ...

Python資料庫操作

我們之前接觸過的儲存資料的方式都是 1.字串 2.列表 3.元組 4.字典 以上方式其實是屬於同一種方式,即將資料儲存在記憶體中 實際在開發過程中,資料儲存主要有三種形式 1.將資料儲存到記憶體中 優點 使用方便,讀寫速度快 缺點 程式關閉的時候,記憶體會釋放,資料會消失 2.將資料寫入到檔案中 優...