python如何操作sql語句,python如何操作SQL語句

2021-12-25 10:00:12 字數 2995 閱讀 2940

1樓:踐道修行歷練

只有用mysqldb這個第三方外掛

在python上怎麼使用sql

2樓:匿名使用者

第一種辦法:

# 匯入sqlite驅動:

>>> import sqlite3

# 連線到sqlite資料庫

# 資料庫檔案是test.db

# 如果檔案不存在,會自動在當前目錄建立:

>>> conn = sqlite3.connect('test.db')

# 建立一個cursor:

>>> cursor = conn.cursor()# 執行一條sql語句,建立user表:

>>> cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')

# 繼續執行一條sql語句,插入一條記錄:

>>> cursor.execute('insert into user (id, name) values (\'1\', \'michael\')')

# 通過rowcount獲得插入的行數:

>>> cursor.rowcount

1# 關閉cursor:

>>> cursor.close()

# 提交事務:

>>> conn.commit()

# 關閉connection:

>>> conn.close()

第二種辦法:

使用 sqlalchemy 等orm 的庫。

python如何操作sql語句

python中sql語句多個 查詢條件的怎麼寫

3樓:匿名使用者

python中有很多字串連線方式,今天在寫**,順便總結一下:

最原始的字串連線方式:str1 + str2

python 新字串連線語法:str1, str2

奇怪的字串方式:str1 str2

% 連線字串:『name:%s; ***: 』 % ('tom', 'male')

字串列表連線:str.join(some_list)

試試這個

results = self.db.query(

'select  lp.id lineproductid,lp.supplierlinetitle,lp.

maintitle,lp.subtitle,lp.showtitle ,lpc.

cityid destinationcityid,\

lpc.cityname destinationcityname,lp.days,lp.

dataflag,lp.ifdel,lp.recomimage_ids as lineproductrecomimage\

from [tczizhuyou].dbo.[zzy_lineproduct] lp with(nolock)\

inner join [tczizhuyou].dbo.[zzy_lineproductcity] lpc with(nolock) on lpc.

lineproduct_id=lp.id and lpc.dataflag=1 and lpc.

isdestination=1 \

python呼叫mysql命令列執行sql語句的問題

4樓:匿名使用者

我以前遇到過

你看看目錄名稱是不是有空格 mysql\mysql server 5.0\

去掉空格

python 向sql語句中傳參

5樓:

一般的處理思路是將資料庫操作的方法放在一個模組中,比如connectsql.py:

import mysqldb

def execnonquery(sql):

conn = mysqldb.connect(host='***x',user='***x',passwd='***x',db='***x')

cur = conn.cursor()

cur.execute(sql)

conn.commit()

conn.close()

那麼你的a.py**為:

from connectsql import *def mysql_insert(i,data):

try:

execnonquery('insert into mytest values(%s,%s)' % (i,data))

except:

return 0

你的b.py**不變。

6樓:匿名使用者

如果將cursor.execute('insert into mytest values(i,data)')

改為cursor.execute('insert into mytest values(?,?)',(i,data,))

會怎麼樣呢

python執行sql插入語句,為什麼這樣會報錯? 22

7樓:day忘不掉的痛

要看你的資料庫裡存的是什麼格式的,如果是unicode的話:

sql="select * from t.branch where name='河南'".decode('utf8')

如果是gb系列編碼的話:

sql="select * from t.branch where name='河南'".decode('utf8').encode('gb18030')

8樓:玩轉資料處理

cursor.execute(sql%param)

簡單SQL查詢 5,蒐集SQL常用的操作語句

updatexset 單價 case when型別 y.列1 then select 列1from ywhere y.x.when型別 y.列2 then select 列2from ywhere y.x.when型別 y.列3 then select 列3from ywhere y.x.else ...

sql語句如何將表進行關聯查詢,sql語句如何將三個表進行關聯查詢?

select distinct a.caseno caseno,a.daterecived,a.buildid,a.contractno,max b.dateofrmi dateofrmi,c.venue,c.district from a left join b on a.caseno b.cas...

sql語句問題,sql語句問題

group by是分組函式 描述可能不準確 count是聚合函式,一定要確定分組的維度,才能在該維度下使用聚合函式進行統計,你要新增dname,那麼dname應該和deptno一起作為統計的維度,又dname在dept表中需要進行表關聯,所以 sql select deptno,dname,coun...