标签: fetchall

迭代 MySQL 连接器游标时 Python 速度很慢

我有以下运行速度非常慢的代码(迭代 57,390 行需要 6.5 秒):

import mysql.connector

cnx=mysql.connector.connect(host=***, ***)
cursorSQL = cnx.cursor()

for dt in date_vect:
        cursorSQL.execute(query2, ((dt[0] + '-00:00:00'), (dt[0] + '-23:59:59'))) 
          #Takes about 0.25sec per iteration
        usr_list = list(cursorSQL.fetchall())  
          #takes about 6.20sec per iteration
Run Code Online (Sandbox Code Playgroud)

正如这里推荐的:Stackoverflow:Python 在...时很慢,我试过:

  • usr_list= cursorSQL.fetchall()
  • usr_list= list(cursorSQL.fetchall())

正如@postoronnim 所建议的,我也尝试过:

  • usr_list= cursorSQL.fetchmany(57390)

没有成功。

然而,有一些缓存效果,因为在第二次运行时相同的迭代只需要 0.5 秒,然后又减慢到 6.5 秒。

  1. 任何想法可能来自哪里?
  2. 你能确认它与我的数据库无关吗,因为所有从MySQL的导入都是在线完成的cursor.executefetchall()缓慢只是由于列表处理?
  3. 你能解释一下为什么会有缓存效应吗?

谢谢。

规格:Python 3.5 | 8核i7 | 16go 公羊

python mysql cursor fetchall mysql-connector-python

5
推荐指数
0
解决办法
1431
查看次数

打印cursor.fetchall() 的结果,不带尾随“L”

我有一个 python 代码,通过 MySQLdb 访问 mysql 来运行 select 语句。然后,我将cursor.fetchall()输出收集为数组,但问题是它L在每个输出的末尾打印一个,如下所示:

sql = "SELECT data1, data2  FROM table1, table2 ;"
cursor.execute(sql)
dataarray = cursor.fetchall()
print dataarray

>>> ((75379L, 45708L), ...)
Run Code Online (Sandbox Code Playgroud)

但在我的表格中,没有 L,只有数字。我怎样才能确保它只获取并打印没有 L 的数据?我宁愿避免[:-1]在字符串上使用,因为这样它会将我的None值更改为Non.

python mysql arrays fetchall mysql-python

5
推荐指数
1
解决办法
6856
查看次数

循环通过fetchall而不知道字段php

我有一个简单的查询,它将从用户表中获取所有

$query = $this->pdo->prepare('SELECT * FROM `users`');
$query->execute();
return $query->fetchAll();
Run Code Online (Sandbox Code Playgroud)

然后我会有一个类似的foreach循环

$results = $User->getUser();
foreach($results as $result){
echo $result['fname'];
echo $result['lname'];
}
Run Code Online (Sandbox Code Playgroud)

但有没有办法显示每个字段而不写每个字段名称?

php foreach pdo fetchall

3
推荐指数
1
解决办法
2621
查看次数

变量在函数内部未定义,无法在函数内部达到MySQL $连接

我有一个代码,当我在页面上使用它时,但我试图使这个功能.我无法让它工作,似乎变量$ customer和$ system没有被发送到代码.即使我在Raw中键入它.有什么想法错了吗?$ Customer是客户的名称,$ system可以是'Source'或'Target'.

function status_total($customer, $system){
        $sql_customer       = "SELECT * FROM `Customer` WHERE Cust_Name = '$customer' LIMIT 0,1";
        $customer_selection = mysqli_query($conn,$sql_customer);
        $customer_row       = mysqli_fetch_assoc($customer_selection);
        $env_lines          = $customer_row["Env_Lines"];
        $cust_id            = $customer_row["Cust_ID"];
        $sql_last_records   = "SELECT * FROM $system WHERE Cust_ID = $cust_id ORDER BY Time DESC LIMIT $env_lines";
        $record_selection   = mysqli_query($conn, $sql_last_records);               

        $result = mysqli_fetch_all($record_selection, MYSQLI_ASSOC);
        $states = array_column($result, "Stat");

        if($states == array_fill(0, count($states), "Run")) {
            echo "Success";
        } else 
            echo "Fail";

    }
Run Code Online (Sandbox Code Playgroud)

https://gist.github.com/R2D2-05/78d81566e4bf0eafd1fa

php mysql mysqli function fetchall

3
推荐指数
1
解决办法
1006
查看次数

我的PHP PDO fetchAll()代码在MySql collumn表上返回一行而不是所有结果

我正在尝试通过PHP和PDO FetchAll()以及Prepared Statements填充来自MySql表的一列的所有结果,但我的代码只返回一行而不是所有结果.我在这里看到了这个问题(PHP PDO返回单行),这与我的问题相似但我没有用这个问题改进我的代码.

我按照PHP手册页面(http://php.net/manual/en/pdostatement.fetchall.php)中的示例尝试将我的代码构造/调整为MySeql表中一列的fetchAll()结果:

PHP手册页 - 带print的FetchAll示例:

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* Fetch all of the remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
Run Code Online (Sandbox Code Playgroud)

我使用HTML select标签基于上面的示例调整了我的代码:

<select name="category" class="form-control" required>
   <option value="0" selected="selected" disabled="disabled" style="display: none">Select one category</option>
   <?php
      require_once 'pdo_connection.php';
      $sth = $dbh-> prepare( 'SELECT * FROM categories' );
      $sth-> execute();
      $result = $sth-> fetchAll(); …
Run Code Online (Sandbox Code Playgroud)

php mysql pdo fetchall

3
推荐指数
1
解决办法
192
查看次数

我如何在PHP中使用fetchAll?

我正在使用此语句来获取我的数据库中的列中的元素

$result = $stmt->fetchAll(PDO::FETCH_COLUMN, 0);
Run Code Online (Sandbox Code Playgroud)

但是在尝试运行它时出现此错误.

调用未定义的方法 mysqli_stmt::fetchAll();

我需要添加到我的php文件中以包含该方法或其他东西吗?

更新1:

class LoginAPI
{
private $db;

function __construct() 
{
    $this->db = new mysqli('xxx', 'xxx', 'xxx', 'xxx');
    $this->db->autocommit(FALSE);
}

function __destruct() 
{
    $this->db->close();
}

function login() 
{
    if(isset($_POST["lang"]))
    {
        $stmt = $this->db->prepare("SELECT code FROM locations");
        $stmt->execute();
        $stmt->bind_result($code);

        //while($stmt->fetch())
        //{
        //  result[] = "code"=>$code;
        //  break;
        //}
        $codeArr = array();
        $result = $stmt->fetch_all();//|PDO::FETCH_GROUP;




        sendResponse(200, json_encode($result));
        return true;
    }
    sendResponse(400, 'Invalid Request');
    return false;
}
}
Run Code Online (Sandbox Code Playgroud)

php sql fetchall

1
推荐指数
1
解决办法
1万
查看次数

使用 PDO 返回数组 - 使用 FetchAll 不起作用

我使用以下代码从数据库中检索数据。问题是它只显示第一行。在这种特殊情况下,这意味着网页上仅显示第一张图片,但我想显示所有图片。

<?php 
    $sql = "SELECT `image-id`, `article-id`, `image-path`, `image-title` FROM `table-images` WHERE `article-id` = :id";

    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(":id", $id);
    $stmt->execute();

    if($result = $stmt->fetch(PDO::FETCH_ASSOC))
    {
?>

<a class="swipebox" href="<?php echo $result['image-path'];?>" title="<?php echo $result['image-title'];?>">
<img alt="image" src="<?php echo $result['image-path'];?>"></a>

<?php
    }// end if
    else {
    echo '0 results';
    }// end else
?>
Run Code Online (Sandbox Code Playgroud)

我读了这篇文章,所以我尝试使用代码:

if($result = $stmt->fetchAll(PDO::FETCH_ASSOC));?

...但这行不通。它甚至不再呼应第一张图片。我在这里缺少什么?

php sql pdo fetchall fetch

1
推荐指数
1
解决办法
6087
查看次数

fetchAll卡住了

我有以下代码:

    $sql = "SELECT table.field, table2.field2 FROM
                table, table2";
    $stmt = $this->db->prepare($sql);
    $stmt->execute();

    $x = $stmt->fetchAll(PDO::FETCH_ASSOC);
Run Code Online (Sandbox Code Playgroud)

我知道这是不够的信息,但我不明白为什么代码只有在我评论fetchAll行时才会执行.当该行执行时,我没有得到任何东西,只是空白浏览器,没有错误或任何东西.在萤火虫中,响应是空白的......它能是什么?

php mysql pdo fetchall

0
推荐指数
1
解决办法
312
查看次数

MySQL PHP - 当mysql字段为空时如何删除不必要的php echo文本?

我有一个客户希望我使用来自mysql数据库的数据在网站页面上格式化业务列表,并使用php将其放在网页上.

客户希望识别每条数据,如下所示:

联系人:Sue Smith
网站:greatwebsite.com

这是问题所在:

有些企业没有网站.所以我不希望商家信息显示如下:

联系人:Sue Smith
网站:

如果没有网站,我不希望网站线显示出来.

这是我到目前为止所做的 - 它没有解决问题(为简洁而截断):

$result = mysql_query("SELECT * FROM businesses ORDER BY business");
while($field = mysql_fetch_array($result))
{
    echo
    "<h3>".$field['business']."</h3>
    <p>Website: ".$field['website']."</p>";
}
Run Code Online (Sandbox Code Playgroud)

如果没有网站,我需要学习如何完全删除"网站"行.

php mysql fetchall

0
推荐指数
1
解决办法
389
查看次数

Python fetchall返回true和false

我有一个python脚本,使fetchall():

connection = pymssql.connect(host='host', user='user', password='pw', database='database', as_dict=True)

cursor = connection.cursor()

cursor.execute("EXEC SPname")
data=cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)

我的问题是cursor.fetchall()返回True或Falses(以及其他列的其他值,例如上面的例子),但在数据库中,值是1或0,当导出为CSV时,它会输入True或False想要1或0.

SQL中返回的示例数据:

ID      Price       Price2      Price3      Active      Opened      Sent        Enable
1      12234.09     1111.09    3444.36      1           1           1           0   
Run Code Online (Sandbox Code Playgroud)

python fetchall

0
推荐指数
1
解决办法
137
查看次数