1. 說明
SQL Server是由Microsoft開發和推廣的關系數據庫管理系統。本文介紹在linux系統下,SQL Server的基本命令。
2. SQLServer基本命令
> sqlcmd -S localhost -U SA -P 密碼 # 用命令行連接
(1) 建庫
> create database testme > go
(2) 看當前數據庫列表
> select * from SysDatabases > go
(3) 看當前數據表
> use 庫名 > select * from sysobjects where xtype='u' > go
(4) 看表的內容
> select * from 表名; > go
3. Python程序訪問SQLServer數據庫
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()