zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    POJ 3041 Asteroids 二分图匹配
    ZOJ 3705 Applications 模拟
    UNIX环境高级编程(第3版)
    明清美文四卷本(共四册)
    卑鄙的圣人:曹操(全10册)
    爱丽丝梦游仙境
    我在大清官场30年
    乌合之众:大众心理研究
    Java多线程编程实战指南
    Linux就该这么学
  • 原文地址:https://www.cnblogs.com/siskin/p/11423631.html
Copyright © 2011-2022 走看看