本文实例为大家分享了Python读取MySQL数据库表数据的具体代码,供大家参考,具体内容如下
环境:Python 3.6 ,Window 64bit
目的:从MySQL数据库读取目标表数据,并处理
代码:
# -*- coding: utf-8 -*-
import pandas as pd
import pymysql
## 加上字符集参数,防止中文乱码
dbconn=pymysql.connect(
host="**********",
database="kimbo",
user="kimbo_test",
password="******",
port=3306,
charset='utf8'
)
#sql语句
sqlcmd="select col_name,col_type,col_desc from itf_datadic_dtl_d limit 10"
#利用pandas 模块导入mysql数据
a=pd.read_sql(sqlcmd,dbconn)
#取前5行数据
b=a.head()
print(b)
# 读取csv数据
# pd.read_csv()
# 读取excel数据
#pd.read_excel()
# 读取txt数据
#pd.read_table()
结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多
思路大概就是,在第一个下拉菜单里中添加change事件的监听,然后在触发change时向后台发送一个ajax请求,然后通过后台返回的内容,动态更新第二个下拉列表
等下给你上例子
我用的是ADODB连接的 SQL Server至于怎么连数据库,你就自己看着办吧
前台
<!doctype html><html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<?php
require_once('../include/SQL_inc/adodb5/adodb.inc.php')
$SQL_connect_str = "PROVIDER=MSDASQLDRIVER={SQL Server}SERVER={KASIM-PC\SQLEXPRESS}DATABASE=mytestUID=saPWD=sa"
$db = NewADOConnection("ado_mssql")
$db->Connect($SQL_connect_str)
$db->Execute("set names utf8")
?>
<script src="../java.inc.bag/jquery-1.9.1.min.js"></script>
<body>
<select id="list1">
<option></option>
<?php
$sqlFlist = "SELECT DISTINCT firstList from list"
$result = $db->Execute($sqlFlist)
while(!$result->EOF){
echo "<option>".mb_convert_encoding($result->fields[0], "UTF-8", "GBK")."</option>"
$result->MoveNext()
}
?>
</select>
<div id="list2box"></div>
</body>
<script>
$("#list1").change(function(e) {
$.ajax({
url:"back.php",
type:"POST",
data:{key:$("#list1").val()},
dataType:"json",
success: function(backdata){
inner = "<select id=\"list2\">"
for(i = 0 i < backdata.length i++)
inner+="<option>"+backdata[i]+"</option>"
inner += "</select>"
$("#list2box").html(inner)
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert(textStatus)
}
})
})
</script>
</html>
后台
<?phpif(isset($_POST['key'])){
require_once('../include/SQL_inc/adodb5/adodb.inc.php')
$SQL_connect_str = "PROVIDER=MSDASQLDRIVER={SQL Server}SERVER={KASIM-PC\SQLEXPRESS}DATABASE=mytestUID=saPWD=sa"
$db = NewADOConnection("ado_mssql")
$db->Connect($SQL_connect_str)
$db->Execute("set names utf8")
$sqlstr = "select dtl from list where firstList = '".mb_convert_encoding($_POST['key'], "GBK", "UTF-8")."'"
$result = $db->Execute($sqlstr)
$backValue = array()
$i = 0
while(!$result->EOF){
$backValue[$i] = $result->fields[0]
$result->MoveNext()
$i++
}
echo json_encode($backValue)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)