欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > Python基于starrocks库连接查询StarRocks数据库

Python基于starrocks库连接查询StarRocks数据库

2024/10/23 15:25:39 来源:https://blog.csdn.net/programmer589/article/details/143134567  浏览:    关键词:Python基于starrocks库连接查询StarRocks数据库

Python基于starrocks库连接查询StarRocks数据库

SQLAlchemy 用法
要使用 SQLAlchemy 连接到 StarRocks,连接字符串如下所示:

starrocks://<User>:<Password>@<Host>:<Port>/<Catalog>.<Database>
import pandas as pd
from sqlalchemy import create_engine, text# 设置 pandas 显示选项以显示所有列
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_colwidth', None)"""
'starrocks://<User>:<Password>@<Host>:<Port>/<Catalog>.<Database>'
"""
def query_user_data(user_name):# 连接到StarRocks数据库engine = create_engine('starrocks://test_user:test_user123@192.168.1.2:9030/sr_db')# 执行查询并获取结果with engine.connect() as connection:sql_query = "select data from sr_db.user where user_name=" + user_nameresult = connection.execute(text(sql_query)).fetchall()# 将查询结果转换为 Pandas DataFrameret_df = pd.DataFrame(result)return ret_df# main function
if __name__ == '__main__':console = Console()user_name = "'tom'"df = query_user_data(user_name )# 如果 DataFrame 不为空,显示if df is not None and not df.empty:print(df)else:print("数据为空")

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com