Linux下操作SQLServer

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()
原文地址:https://www.cnblogs.com/siskin/p/11423631.html