C++连接MySQL 操作的封装

2023-05-16

以前写过一篇 c++连接mysql 的博客, 每次都要写类来封装数据库的操作函数, 参考一位大佬的封装,不说了,直接上代码:
头文件如下:

#pragma once

#include <windows.h>
#include <iostream>
#include <mysql.h>
#include <stdint.h>
#include <string>
#include <algorithm>
#include <map>
#include <iostream>
#include <sstream>

class MySQLConnection
{

public:
    MySQLConnection();
    ~MySQLConnection();
    // connects to a MySQL-server
    bool Connect(const std::string &sHostname, const uint16_t &wPort, const std::string &sUsername, const std::string &sPassword, const std::string &sDB);
    // selects a DB
    bool SelectDB(const std::string &sSchemaName);
    // disconnects (huh, who guessed this?)
    void Disconnect();
    // returns the last error string
    const std::string GetLastError() const;
    // gets a pointer to the MySQL connection
    MYSQL *getConn();
    // returns true when connected
    bool IsConnected();
    // returns an escaped string
    const std::string EscapeString(const std::string &value) const;

private:
    MYSQL *m_pMySQLConn;
    bool m_bIsConnected;
    std::string m_sHostname;
    std::string m_sUsername;
    std::string m_sPassword;
    std::string m_sSchemaName;
    uint16_t m_wPort;
};

class MySQLQuery
{
public:
    MySQLQuery(MySQLConnection *mConn, const std::string &sStatement);
    ~MySQLQuery();

    // sets the value of idx to a given string (also adds quotation marks and escapes the string)
    bool setString(const unsigned int &idx, const std::string &value);
    // sets the value of idx to a given int
    bool setInt(const unsigned int &idx, const int &value);
    // sets the value of idx to a given double
    bool setDouble(const unsigned int &idx, const double &value);
    // sets the value of idx to a given time_t
    bool setTime(const unsigned int &idx, const time_t &value);
    // sets the value of idx to NULL
    bool setNull(const unsigned int &idx);

    // executes a SELECT-statement
    bool ExecuteQuery();
    // executes an UPDATE-statement
    bool ExecuteUpdate();
    // executes an INSERT-statement and returns the last inserted ID
    bool ExecuteInsert();

    //execute a DELETE-statement
    bool ExecuteDelete();

    // builds the query string with filled-in arguments and returns it
    const std::string BuildQueryString();

    // returns a field name
    const std::string getFieldName(const unsigned int &field);
    // gets a string value from the given row and field
    const std::string getString(const unsigned int &row, const unsigned int &field);
    const std::string getString(const unsigned int &row, const std::string &field);
    // gets an int value from the given row and field
    int getInt(const unsigned int &row, const unsigned int &field);
    int getInt(const unsigned int &row, const std::string &field);
    // gets a double value from the given row and field
    double getDouble(const unsigned int &row, const unsigned int &field);
    double getDouble(const unsigned int &row, const std::string &field);
    // gets a time value from the given row and field
    time_t getTime(const unsigned int &row, const unsigned int &field);
    time_t getTime(const unsigned int &row, const std::string &field);

    // returns the result row count
    unsigned int GetResultRowCount();
    unsigned int GetFieldCount();


private:
    typedef std::map<int, std::string> TResultRow;
    MySQLConnection *m_sqlConn;
    int m_iResultRowCount;
    std::string m_sStatement;
    std::map<int, std::string> m_mArgMap;
    std::map<int, TResultRow> m_mResultMap;
    std::map<int, std::string> m_mFieldMap;
    std::map<std::string, int> m_mFieldStringToIntMap;

};

源文件如下:


#include "MySqlQuery.h"
#include <cassert>


MySQLConnection::MySQLConnection()
{
    m_bIsConnected = false;
    m_pMySQLConn = NULL;
    m_sHostname = "";
    m_sUsername = "";
    m_sPassword = "";
    m_wPort = 0;
    m_sSchemaName = "";
}

MySQLConnection::~MySQLConnection()
{
    if(m_pMySQLConn != nullptr)
    {
        std::clog << "Closing MySQL Connection" << std::endl;
        mysql_close(m_pMySQLConn);
    }
}

bool MySQLConnection::Connect(const std::string &sHostname, const uint16_t &wPort, 
	const std::string &sUsername, const std::string &sPassword, const std::string &sDB = NULL)
{
    // If we're already connected, we should close the first connection
    Disconnect();

    m_sHostname = sHostname;
    m_sUsername = sUsername;
    m_sPassword = sPassword;
    m_wPort = wPort;
    m_sSchemaName = sDB;
    m_bIsConnected = false;

    MYSQL *pMySQLConnRet = nullptr;
    m_pMySQLConn = mysql_init(m_pMySQLConn);

    std::clog << "Connection to " << m_sUsername << "@" << m_sHostname << ":" << wPort << "..." << std::endl;

    pMySQLConnRet = mysql_real_connect(m_pMySQLConn, m_sHostname.c_str(), m_sUsername.c_str(), m_sPassword.c_str(), m_sSchemaName.c_str(), m_wPort, NULL, 0);

    if(nullptr == pMySQLConnRet)
    {
        m_bIsConnected = false;
        std::cerr << "Connection failed: " << mysql_error(m_pMySQLConn);
    } 
    else 
    {
        m_bIsConnected = true;
        std::clog << "Connected!" << std::endl;
    }

    return m_bIsConnected;
}

void MySQLConnection::Disconnect()
{
    if(m_bIsConnected)
    {
        mysql_close(m_pMySQLConn);
        m_pMySQLConn = nullptr;
        std::clog << "Disconnected from MySQL DB!" << std::endl;
    }


    m_bIsConnected = false;

}

bool MySQLConnection::SelectDB(const std::string &sSchemaName)
{
    if(!m_bIsConnected)
    {
        std::cerr << "Not connected to MySQL DB!" << std::endl;
        return false;
    }

    if(mysql_select_db(m_pMySQLConn, sSchemaName.c_str()) != 0)
    {
        std::cerr << "Failed to select DB! Error: " << mysql_error(m_pMySQLConn) << std::endl;
        return false;
    } 
    else 
    {
        m_sSchemaName = sSchemaName.c_str();
        std::clog << "Selected database \"" << sSchemaName << "\"" << std::endl;
        return true;
    }
}

const std::string MySQLConnection::GetLastError() const
{
    if(!m_bIsConnected)
    {
        std::cerr << "Not connected to MySQL DB!" << std::endl;
        return "Not connected";
    }

    return (char*)mysql_error(m_pMySQLConn);
}

MYSQL *MySQLConnection::getConn()
{
    return m_pMySQLConn;
}

bool MySQLConnection::IsConnected()
{
    return m_bIsConnected;
}

const std::string MySQLConnection::EscapeString(const std::string &_strValue) const
{
    if(!m_bIsConnected)
    {
        std::cerr << "Not connected to MySQL DB!" << std::endl;
        return "";
    }

    char *pValue = new char[(_strValue.length()*2)+1];
    mysql_real_escape_string(m_pMySQLConn, pValue, _strValue.c_str(), _strValue.length());

    std::string sRet = pValue;
    delete[] pValue;
    pValue = nullptr;

    return sRet;
}

MySQLQuery::MySQLQuery(MySQLConnection *pConn, const std::string &strStatement)
{
    m_sqlConn = pConn;
    m_sStatement = strStatement;
    m_iResultRowCount = 0;

    int argCount = std::count(m_sStatement.begin(), m_sStatement.end(), '?');
    for(int i = 1; i <= argCount; i++)
    {
        m_mArgMap.insert(std::pair<int, std::string>(i, ""));
    }
}

MySQLQuery::~MySQLQuery()
{
}

bool MySQLQuery::setString(const unsigned int &idx, const std::string &value)
{

    if(idx > m_mArgMap.size())
    {
        std::cerr << "Index exceeds total arg count in statement" << std::endl;
        return false;
    }

    std::stringstream ss;
    std::string escapedValue = m_sqlConn->EscapeString(value);
    ss << "\"" << escapedValue << "\"";
    m_mArgMap[idx] = ss.str();

    return true;
}

bool MySQLQuery::setInt(const unsigned int &idx, const int &value)
{
    if(idx > m_mArgMap.size())
    {
        std::cerr << "Index exceeds total arg count in statement" << std::endl;
        return false;
    }

    std::stringstream ss;
    ss << value;
    m_mArgMap[idx] = ss.str();

    return true;
}

bool MySQLQuery::setDouble(const unsigned int &idx, const double &_dValue)
{
    if(idx > m_mArgMap.size())
    {
        std::cerr << "Index exceeds total arg count in statement" << std::endl;
        return false;
    }

    std::stringstream ss;
    ss << _dValue;
    m_mArgMap[idx] = ss.str();

    return true;
}

bool MySQLQuery::setTime(const unsigned int &idx, const time_t &value)
{
    if(idx > m_mArgMap.size())
    {
        std::cerr << "Index exceeds total arg count in statement" << std::endl;
        return false;
    }

    std::stringstream ss;
    ss << value;
    m_mArgMap[idx] = ss.str();

    return true;
}

bool MySQLQuery::setNull(const unsigned int &idx)
{
    if(idx > m_mArgMap.size())
    {
        std::cerr << "Index exceeds total arg count in statement" << std::endl;
        return false;
    }

    m_mArgMap[idx] = "NULL";
    return true;
}

const std::string MySQLQuery::getFieldName(const unsigned int &iField)
{
    if(iField < 1)
    {
        std::cerr << "The field index has to be over 1!" << std::endl;
        return "";
    }
    else if(m_mFieldMap.size() < iField)
    {
        std::cerr << "There are only " << m_mFieldMap.size() << " fields available!" << std::endl;
        return "";
    }

    std::string sFieldName = m_mFieldMap[iField];
    return sFieldName;
}

const std::string MySQLQuery::getString(const unsigned int &row, const unsigned int &iField)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return "";
    } 
    else if(GetResultRowCount() < row)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return "";
    } 
    else if(row < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return "";
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[row-1];

    std::string sValue = rSelectedRow[iField];

    return sValue;
}

const std::string MySQLQuery::getString(const unsigned int &iRow, const std::string &strField)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return "";
    }
    else if(GetResultRowCount() < iRow)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return "";
    } 
    else if(iRow < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return "";
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[iRow-1];

    int iFieldID = m_mFieldStringToIntMap[strField];
    std::string sValue = rSelectedRow[iFieldID];

    return sValue;
}

int MySQLQuery::getInt(const unsigned int &iRow, const unsigned int &iField)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return -1;
    } 
    else if(GetResultRowCount() < iRow)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return -1;
    } 
    else if(iRow < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return -1;
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[iRow-1];

    int iValue = atoi(rSelectedRow[iField].c_str());

    return iValue;
}

int MySQLQuery::getInt(const unsigned int &iRow, const std::string &strField)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return -1;
    } 
    else if(GetResultRowCount() < iRow)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return -1;
    } 
    else if(iRow < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return -1;
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[iRow-1];

    int iFieldID = m_mFieldStringToIntMap[strField];
    int iValue = atoi(rSelectedRow[iFieldID].c_str());

    return iValue;
}

double MySQLQuery::getDouble(const unsigned int &iRow, const unsigned int &iField)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return -1.0;
    } 
    else if(GetResultRowCount() < iRow)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return -1.0;
    } 
    else if(iRow < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return -1.0;
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[iRow-1];

    double dValue = atof(rSelectedRow[iField].c_str());

    return dValue;
}

double MySQLQuery::getDouble(const unsigned int &iRow, const std::string &strField)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return -1.0;
    } 
    else if(GetResultRowCount() < iRow)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return -1.0;
    } 
    else if(iRow < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return -1.0;
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[iRow-1];

    int iFieldID = m_mFieldStringToIntMap[strField];
    double dValue = atof(rSelectedRow[iFieldID].c_str());

    return dValue;
}

time_t MySQLQuery::getTime(const unsigned int &row, const unsigned int &field)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return -1;
    } 
    else if(GetResultRowCount() < row)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return -1;
    } 
    else if(row < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return -1;
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[row-1];

    time_t tValue = atoi(rSelectedRow[field].c_str());

    return tValue;
}

time_t MySQLQuery::getTime(const unsigned int &row, const std::string &field)
{
    if(GetResultRowCount() < 1)
    {
        std::cerr << "The query didn't return any rows!" << std::endl;
        return -1;
    } 
    else if(GetResultRowCount() < row)
    {
        std::cerr << "There are only " << GetResultRowCount() << " rows available!" << std::endl;
        return -1;
    } 
    else if(row < 1)
    {
        std::cerr << "The selected row has to be > 1" << std::endl;
        return -1;
    }

    TResultRow rSelectedRow;
    rSelectedRow = m_mResultMap[row-1];

    int iFieldID = m_mFieldStringToIntMap[field];
    time_t tValue = atoi(rSelectedRow[iFieldID].c_str());

    return tValue;
}


unsigned int MySQLQuery::GetResultRowCount()
{
    const int iRowCount = m_mResultMap.size();
    return iRowCount;
}

unsigned int MySQLQuery::GetFieldCount()
{
    const int iFieldCount = m_mFieldMap.size();
    return iFieldCount;
}

const std::string MySQLQuery::BuildQueryString()
{
    // replace each '?' with the corresponding value
    int iLastFoundPos = 0;
    std::string sPreparedStatement;
    sPreparedStatement = m_sStatement;
    for(unsigned int i = 1; i <= m_mArgMap.size(); i++)
    {
        iLastFoundPos = sPreparedStatement.find('?');
        sPreparedStatement.replace(iLastFoundPos, 1, "");
        sPreparedStatement.insert(iLastFoundPos, m_mArgMap[i]);
    }

    return sPreparedStatement;
}

bool MySQLQuery::ExecuteQuery()
{
    std::string sStatement = BuildQueryString();

    if(mysql_query(m_sqlConn->getConn(), sStatement.c_str()))
    {
        std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
        return false;
    }

    MYSQL_RES *pResult = mysql_store_result(m_sqlConn->getConn());

    if(pResult == NULL)
    {
        std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
        return false;
    }


    int iNumFields = mysql_num_fields(pResult);
    MYSQL_ROW row;
    MYSQL_FIELD *field;

    // Get field names and store it in the map
    int i = 0;
    while((field = mysql_fetch_field(pResult)))
    {
        m_mFieldMap.insert(std::pair<int, std::string>(i, field->name));
        m_mFieldStringToIntMap.insert(std::pair<std::string, int>(field->name, i));
        i++;
    }

    // Get Rows
    i = 0;
    while((row = mysql_fetch_row(pResult)))
    {
        TResultRow resRow;
        for(int n = 0; n < iNumFields; n++)
        {
            resRow.insert(std::pair<int, std::string>(n, row[n] ? row[n] : "NULL"));
        }

        m_mResultMap.insert(std::pair<int, TResultRow>(i, resRow));

        i++;
    }

    return true;
}

bool MySQLQuery::ExecuteUpdate()
{
    std::string sStatement = BuildQueryString();

    if(mysql_query(m_sqlConn->getConn(), sStatement.c_str()))
    {
        std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
        return false;
    }

    return true;
}

bool MySQLQuery::ExecuteInsert()
{
    std::string sStatement = BuildQueryString();

    if(mysql_query(m_sqlConn->getConn(), sStatement.c_str()))
    {
        std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
        return false;
    }

    return true;
}

bool MySQLQuery::ExecuteDelete()
{
	std::string sStatement = BuildQueryString();

	if (mysql_query(m_sqlConn->getConn(), sStatement.c_str()))
	{
		std::cerr << "MySQL Error: " << m_sqlConn->GetLastError() << std::endl;
		return false;
	}

	return true;
}

测试代码如下:

#include "mysqlquery.h"
#include <time.h>
#include <iostream>
using namespace std;

int main()
{

	MySQLConnection mySqlCon;

	if (!mySqlCon.Connect("127.0.0.1", 3306, "root", "123456", "wd"))
	{
		cout << "Error db connection " << endl;
		return 0;
	}

	cout << "Connection Success " << endl;

	MySQLQuery query(&mySqlCon, "SELECT * FROM tbl_student;");
	if (query.ExecuteQuery())
	{
		unsigned int row = query.GetResultRowCount();
		unsigned int col = query.GetFieldCount();
		for (unsigned int r = 0; r < row; ++r)
		{
			cout << "name = " << query.getString(r + 1, "name")
				 << ", age = " << query.getString(r + 1, "age")
				 << ", sex = " << query.getString(r + 1, "sex") << endl;
		}
	}

	MySQLQuery insertQuery(&mySqlCon, "INSERT INTO tbl_student VALUES ('shihan', '12', 1); ");
	if (insertQuery.ExecuteInsert())
	{
		cout << "Success Query Insert" << endl;
	}

	MySQLQuery updateQuery(&mySqlCon, "UPDATE tbl_student SET sex = 1 WHERE  name = 'shihan'; ");
	if (updateQuery.ExecuteUpdate())
	{
		cout << "Success Query Update" << endl;
	}

	MySQLQuery deleteQuery(&mySqlCon, "DELETE FROM tbl_student WHERE  name = 'shihan'; ");
	if (deleteQuery.ExecuteDelete())
	{
		cout << "Success Query Delete" << endl;
	}

	MySQLQuery query1(&mySqlCon, "SELECT * FROM tbl_student;");
	if (query1.ExecuteQuery())
	{
		unsigned int row = query.GetResultRowCount();
		unsigned int col = query.GetFieldCount();
		for (unsigned int r = 0; r < row; ++r)
		{
			cout << "name = " << query.getString(r + 1, "name")
				<< ", age = " << query.getString(r + 1, "age")
				<< ", sex = " << query.getString(r + 1, "sex") << endl;
		}
	}

	mySqlCon.Disconnect();


	
	return 0;
}

有写的不对的,欢迎大佬们指导。。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++连接MySQL 操作的封装 的相关文章

  • 需要有关使用 PHP 在 mysql 数据库中插入逗号分隔数据的帮助

    数据库表中已有的演示数据 INSERT INTO csvtbl ID SKU Product Name Model Make Year From Year To VALUES 1 C2AZ 3B584 AR Power Steering P
  • MySQL 子查询返回多行

    我正在执行这个查询 SELECT voterfile county Name voterfile precienct PREC ID voterfile precienct Name COUNT SELECT voterfile voter
  • 从数据库生成 XML 时出现 PHP 编码错误 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我正在尝试获取一个简单的 PHP 服
  • 更改Docker容器中的mysql密码

    我如何更改 docker 容器中的 root 密码 因为一旦我停止 mysql 服务 容器就会自动停止 我应该停止 mysql 容器并部署一个新容器吗 您可以使用正在运行的容器更改它docker exec session https doc
  • MySQL“选择更新”行为

    根据 MySql 文档 MySql 支持多粒度锁定 MGL case 1 开放航站楼 1 连接到mysql mysql gt start transaction Query OK 0 rows affected 0 00 sec mysql
  • php 崩溃后 mysql 表被锁定

    我有一个 MySQL DB 和一个 innoDB 表 我有一个 php 页面 用于连接 锁定表 进行一些更新 然后解锁表 PHP 页面通过 wamp 与 apache 一起提供 php页面将文件上传到数据库 我决定通过上传一个大小大于分配给
  • 如何使用实体框架设置连接字符串

    我将 EF6 与 MySQL 结合使用 并有一个用于多个数据库的模型 我希望能够在我的表单中设置连接设置 如何以编程方式设置模型的连接字符串 你应该使用EntityConnectionFactory这就是您所需要的 public strin
  • 本地数据库缓存的最佳实践?

    我正在开发一个应用程序 该应用程序的部分内容依赖于 MySQL 数据库 在某些情况下 应用程序将在互联网连接 UMTS 有限的环境中运行 特别是延迟较高的环境 应用程序的用户能够登录 并且应用程序用户界面的大部分内容都是从 MySQL 数据
  • 基本表创建 fpdf

    我找不到使用 fpdf 制作表格并从 mysql 数据库获取数据的合适教程 我只是想知道如何创建一个 我在网上尝试示例时遇到了很多错误 例如 我有 名字 中间名 姓氏 年龄 和 电子邮件 列 如何使用 fpdf 创建表格并回显数据库中的条目
  • 从 PDO 准备好的语句中获取原始 SQL 查询字符串

    在准备好的语句上调用 PDOStatement execute 时 有没有办法让原始 SQL 字符串执行 出于调试目的 这将非常有用 我假设您的意思是您想要最终的 SQL 查询 并将参数值插入其中 我知道这对于调试很有用 但这不是准备好的语
  • 从数据库 MYSQL 和 Codeigniter 获取信息

    如果你们需要其他信息 上一个问题就在这里 从数据库中获取信息 https stackoverflow com questions 13336744 fetching information from the database 另一个更新 尽
  • 如何使用 PHP 从 MySQL 检索特定值?

    好吧 我已经厌倦了 过去一周我花了大部分空闲时间试图解决这个问题 我知道 SQL 中的查询已更改 但我无法弄清楚 我能找到的所有其他帖子似乎都已经过时了 如果有人能帮助我 我将非常感激 我想做的就是使用手动输入数据库的唯一 密码 来检索行的
  • “警告:mysql_query():提供的参数不是有效的 MySQL-Link” - 为什么?

    我的代码有什么问题吗 我不断收到此错误 Warning mysql query supplied argument is not a valid MySQL Link resource in functions php on line 4
  • 如何在数据库中保存未来(!)日期

    这个问题专门涉及未来的日期和时间 对于过去的值 UTC 无疑是首选 我想知道是否有人对拯救生命的 最佳 方法有建议futureMySQL 数据库中的日期和时间 或者就此而言一般来说 特别是在该列可以保存不同时区时间的情况下 考虑到时区规则可
  • 将 mysql LONGTEXT 值转换为 VARCHAR 值?

    我有一个在用户 Facebook 墙上发布的功能 我发送到 facebook 的一件事是我从设置为 LONGTEXT 的 mysql 表中获取的一些文本 如果我将表设置为 LONGTEXT 则文本不会发送到 facebook 但如果我将表设
  • 项目链接在 Wamp 服务器上不起作用

    我正在另一台计算机上安装 Wamp 服务器来运行中型数据库和 UI 我已成功阻止 IIS 并将服务器路由到 Localhost 8080 但是每当我尝试从 localhost 主页访问我的项目时 在 www 文件中 我被重定向到页面未找到错
  • 如何在 MacOS 上卸载 Mysql Shell

    我错误地安装了 MySql Shellhttps dev mysql com doc mysql shell 8 0 en https dev mysql com doc mysql shell 8 0 en 在我的 MacBook Pro
  • ORDER BY 之后的 GROUP BY

    我需要去做GROUP BY after ORDER BY 我不明白为什么 MySQL 不支持这一点 这是我的代码 SELECT pages id contents id language ORDER BY FIND IN SET langu
  • 如何编写 bash 函数来包装另一个命令?

    我正在尝试编写一个函数包装器mysql command If my cnf存在于 pwd 中 我想自动附加 defaults file my cnf到命令 这就是我正在尝试的 function mysql if e my cnf then
  • PHP + MySQL 队列

    我需要一个充当队列的简单表 我的 MySQL 服务器限制是我不能使用 InnoDB 表 只能使用 MyISAM 客户 工人将同时工作 他们每次都需要接受不同的工作 我的想法是执行以下操作 伪代码 job lt SELECT FROM que

随机推荐