基于Python3的接⼝⾃动化总结(四)——Vertica数据库通过python连接Vertica数据库
#!/usr/bin/env python3
# -*-coding:utf-8-*-
import vertica_python
class Vertica:
def__init__(self):
def_connect(self, database, user, password, host, port):
python在线编辑器python3
def connect(self, database, user, password, host, port):
try:
tion:
conn_info = tion.options
if conn_info['database']== database and conn_info['user']== user and conn_info[
'password']== password and conn_info['host']== host and conn_info['port']== port:
print(f"返回已存在的数据库连接 vertica://{host}:{port}/{database}成功,⽤户名:{user}")
divine护肤品什么牌子else:
self._connect(database, user, password, host, port)
else:
self._connect(database, user, password, host, port)
except Exception as e:
print(f"连接数据库 vertica://{host}:{port}/{database}失败,⽤户名:{user},异常信息:{e.args[0]}")
plane是什么意思?def close(self):
tion:
print(f"关闭据库成功")
def execute_sql_list(self, database, user, password, host, port, sql):
"""
数据库查询,返回为列表
:param database: 数据库
:param user:⽤户名
:param password:密码
:
param host: 服务器ip
:param port: 端⼝
:param sql: 数据库语句
:return:
"""
share花体英文
row =None
try:
cur = tion.cursor()
row = cur.fetchall()# all rows in table
print(f"执⾏sql语句成功:{sql}")
cur.close()
except Exception as e:
print(f"执⾏sql语句失败:{sql},异常信息:{e.args[0]}")
finally:
print(f"sql语句执⾏结果:{str(row)}")
self.close()
return row
def execute_sql_dict(self, database, user, password, host, port, sql):
def execute_sql_dict(self, database, user, password, host, port, sql):
免费简历
"""
数据库查询,返回为字典
:param database: 数据库
:param user:⽤户名
:param password:密码
:param host: 服务器ip
:param port: 端⼝fortify词根
:param sql: 数据库语句
:return:
"""
row =None
try:
cur = tion.cursor('dict')
row = cur.fetchall()# all rows in table
print(f"执⾏sql语句成功:{sql}")
cur.close()
except Exception as e:
print(f"执⾏sql语句失败:{sql},异常信息:{e.args[0]}")
finally:
print(f"sql语句执⾏结果:{str(row)}")
self.close()
return row
def execute_sql(self, database, user, password, host, port, sql):# 数据库操作,⽆返回值
"""
数据库操作,⽆返回值
:param database: 数据库
:param user:⽤户名
:param password:密码
:param host: 服务器ip
:param port: 端⼝
:
param sql: 数据库插⼊语句
:return:
"""
try:
cur = tion.cursor()
print(f"执⾏sql语句成功:{sql}")
cur.close()
except Exception as e:
print(f"执⾏sql语句失败:{sql},异常信息:{e.args[0]}")
finally:
self.close()
Tips
1.python连接Vertica数据库后如何处理时间字段
insert into birth VALUES('zhangsan', to_date('2014-05-26','yyyy-MM-dd'));
or
insert into birth VALUES('zhangsan', to_date('2014-05-26 02:00:00','yyyy-MM-dd hh24:mi:ss'));
注:
DATE8 Represents a month,day,and year
TIMESTAMP8 Represents a date and time without timezone
1.vertica时间类型为date的不精确到⼩时分秒, 如果需要,定义的时间数据类型必须为timestamp。
2.同样,查询,vertica的to_date()函数不精确到⼩时分秒,如果需要,需要⽤to_timestamp()函数。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。