SQL Server
SQL Server是由Microsoft开发和推广的关系数据库管理系统.
SQL Server特点
- C/S体系结构
- 具有很好的伸缩性
- 提供数据仓库功能
- 支持OLE DB和多种查询
代码实现SQL server创建、插入、查询、删除操作
需要pymssql库支持
import pymssql
import time
import traceback, sys
class database(object):
"""数据库操作对象"""
def __init__(self, url,username,password,databaseName):
self.url = url
self.username = username
self.password = password
self.databaseName = databaseName
self.connect = self.sql_server_conn()
self.cursor = self.connect.cursor()
def sql_server_conn(self):
connect = pymssql.connect(self.url,self.username,self.password,self.databaseName) #服务器名,账户,密码,数据库名
if connect:
print(u"Success!!")
return connect
#查看表的所有字段,
#@table_name :表名
def get_column_name(self,table_name):
self.cursor.execute("select top 1 * from " + table_name) #执行sql语句
data_dict=[]
for field in self.cursor.description:
data_dict.append(field[0])
print(data_dict)
return data_dict
#得到数据库所有的表名
def get_table_name(self):
sql = "SELECT NAME FROM SYSOBJECTS WHERE XTYPE='U' ORDER BY NAME"
self.cursor.execute(sql) # 返回执行成功的结果条数
rows = self.cursor.fetchall()
for d in rows:
for k in d:
print(k)
#执行sql语句,增删改查
#@sql:sql语句
def execute_sql(self,sql):
sql = sql.lower()
if 'insert' in sql or 'delete' in sql or 'update' in sql:
self.cursor.execute(sql)
self.connect.commit()
return
elif 'select' in sql :
self.cursor.execute(sql)
rows = self.cursor.fetchall()
for k in rows:
print(k)
return rows
#关闭游标,连接
def close(self):
self.cursor.close() #关闭游标
self.connect.close()