可从微软官网获取详细步骤
https://docs.microsoft.com/zh-cn/sql/linux/sql-server-linux-overview?view=sql-server-2017
SQL Server 2017—— Linux上的 SQL Server——快速入门——安装和连接
连接数据库
保存一段代码,下次连接数据库更方便
默认本地服务器名:localhost (也可使用ip地址)
默认用户名:SA
密码:自行设定
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bashrc
source ~/.bashrc
sqlcmd -S localhost -U SA -P '密码'
查看数据库列表
1> SELECT Name from sys.Databases
1> go
查询表
1> SELECT * FROM INFORMATION_SCHEMA.TABLES
2> go
也可以用下列参数代替 * ,输出部分信息
TABLE_CATALOG
TABLE_SCHEMA
TABLE_NAME
TABLE_TYPE
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
具体安装包上官网查找 https://www.python.org/
mkdir /usr/local/python3
tar -zxvf Python-3.7.0.tgz
cd Python-3.7.0
./configure
若出错 可能因为gcc没有安装
yum -y install gcc
yum -y install gcc-c++
运行后会提醒
If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations
可以运行 ./configure --enable-optimizations 进行配置优化
进一步安装
make && make install
ln -s /usr/local/python3/python3.7.0/python /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
具体文件位置可能有差异
补充:查询pip位置
find / -name pip3*
python3 -V
pip3 -V
sudo pip3 install pymssql
了解更多:http://www.pymssql.org/en/stable/intro.html#getting-started
vim TestDB.py
点击i到插入模式,然后就可以进行代码的编写
代码示例
from os import getenv
import pymssql
conn = pymssql.connect(server='localhost', user='SA', password='密码', database="TestDB",charset='utf8')
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.')])
# you must call commit() to persist your data if you don't set autocommit to True
conn.commit()
cursor.execute('SELECT * FROM persons WHERE salesrep LIKE %s','J%')
row = cursor.fetchone()
while row:
print("ID=%d, Name=%s" % (row[0], row[1]))
row = cursor.fetchone()
conn.close()
点击ESC输入:wq (保存并退出)
python3 TestDB.py