1. 說(shuō)明
?SQL Server是由Microsoft開(kāi)發(fā)和推廣的關(guān)系數(shù)據(jù)庫(kù)管理系統(tǒng)。本文介紹在linux系統(tǒng)下,SQL Server的基本命令。
2. SQLServer基本命令
> sqlcmd -S localhost -U SA -P 密碼 # 用命令行連接
(1) 建庫(kù)
> create database testme
> go
(2) 看當(dāng)前數(shù)據(jù)庫(kù)列表
> select * from SysDatabases
> go
(3) 看當(dāng)前數(shù)據(jù)表
> use 庫(kù)名
> select * from sysobjects where xtype='u'
> go
(4) 看表的內(nèi)容
> select * from 表名;
> go
3. Python程序訪問(wèn)SQLServer數(shù)據(jù)庫(kù)
圖片
import pymssql
server = 'localhost'
user = 'sa'
password = 密碼
database = 'ecology'
conn = pymssql.connect(server, user, password, database)
cursor = conn.cursor()
cursor.execute("""
IF OBJECT_ID('persons', 'U') IS NOT NULL
DROP TABLE persons
CREATE TABLE persons (
id INT NOT NULL,
name VARCHAR(100),
salesrep VARCHAR(100),
PRIMARY KEY(id)
)
""")
cursor.executemany(
"INSERT INTO persons VALUES (%d, %s, %s)",
[(1, 'John Smith', 'John Doe'),
(2, 'Jane Doe', 'Joe Dog'),
(3, 'Mike T.', 'Sarah H.')])
conn.commit()
cursor.execute('SELECT * FROM persons WHERE salesrep=%s', 'John Doe')
row = cursor.fetchone()
while row:
print("ID=%d, Name=%s" % (row[0], row[1]))
row = cursor.fetchone()
conn.close()
掃描二維碼推送至手機(jī)訪問(wèn)。
版權(quán)聲明:本文由信途科技轉(zhuǎn)載于網(wǎng)絡(luò),如有侵權(quán)聯(lián)系站長(zhǎng)刪除。
轉(zhuǎn)載請(qǐng)注明出處http://macbookprostickers.com/xintu/22565.html