我是靠谱客的博主 激情网络,这篇文章主要介绍python从数据库中获得数据到numpy,现在分享给大家,希望可以做个参考。

 

使用mysql.connector连接mysql数据库

  
  
import mysql.connector from itertools import chain import sys, os import numpy as np user = ' root ' pwd = ' root ' host = ' 127.0.0.1 ' db = ' lending ' select_sql = " SELECT id,age FROM mytable " count_sql = " SELECT count(*) FROM mytable " cnx = mysql.connector.connect(user = user, password = pwd, host = host, database = db) cursor = cnx.cursor() try : cursor.execute(count_sql) count = 0 # 显示总数 for row1 in cursor: count = row1[0]; print (row1) test = np.arange(count * 2 ).reshape(count, 2 ) numrows = cursor.execute(select_sql) print ( " ================================ " ) index = 0; for row in cursor: print (row) test[index,:] = np.fromiter(row, dtype = (int,int), count = 1 ) index = index + 1 print (test) except mysql.connector.Error as err: print ( " query table 'mytable' failed. " ) print ( " Error: {} " .format(err.msg)) sys.exit() cnx.commit() cursor.close() cnx.close()

Numpy常见数据类型

image

 

 
 

最后

以上就是激情网络最近收集整理的关于python从数据库中获得数据到numpy的全部内容,更多相关python从数据库中获得数据到numpy内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部