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 操作的封装 的相关文章

  • Android 应用程序和 MySql 连接无法连接。打开

    当我尝试打开连接时发生错误并显示 System TypeInitializationException MySql Data MySqlClient Replication ReplicationManager 的类型初始值设定项引发异常
  • 无法使用 PDO 插入 MySQL 数据库...没有错误

    我遇到一个问题 无法使用 PDO 将任何内容插入 MySQL 数据库 我没有收到任何错误 但每当我检查数据库是否已插入行时 表都是空的 我知道我有一个到数据库的连接 因为我可以选择但不能插入 这是我扩展 PDO 的类 class Datab
  • mysql故障转移:如何选择slave作为新的master?

    我是 mysql 新手 当涉及到故障转移时 哪个从机应该晋升为新的主机 例如 A是master B和C是slave A对B和C进行异步复制 在某个时间点 B 从 A 接收的数据多于 C A 崩溃 如果我们将C提升为新的master 并将B的
  • 检索MySQL中特定日期范围内发生的所有记录

    我有一个包含多个合同的表 每个合同都有一个开始日期和一个结束日期 如下所示 ID Contract Name Start Date End Date 1 Joe Bloggs 2012 01 01 2012 02 05 2 John Smi
  • 如何将两个django模型(表)合并为一个模型(表)

    我想合并两个 django 模型并创建单个模型 我们假设 我有第一个表表 A 其中包含一些列和数据 Table A col1 col2 col3 col4 x1 x2 x3 x4 y1 y2 y3 y4 我还有另一个表 Table B 其中
  • MySQL Match() Against() 区分大小写

    目前 我的数据库是字符集Latin1意义 SELECT FROM TABLE MATCH column1 AGAINST words here IN BOOLEAN MODE 只会返回不敏感的搜索 但问题是我的数据库将通过不敏感和区分大小写
  • Hibernate 可以使用 MySQL 的“ON DUPLICATE KEY UPDATE”语法吗?

    MySQL 支持 INSERT ON DUPLICATE KEY UPDATE 语法允许您 盲目 插入数据库 并回退到更新现有记录 如果存在 当您想要快速事务隔离并且想要更新的值取决于数据库中已有的值时 这非常有用 作为一个人为的示例 假设
  • 通过我的java代码导出数据库

    我想使用我的 java 代码导出我的 MySQL 数据库 但我还没有找到任何办法 我想要做的就是我的应用程序中有一个按钮作为 导出数据库 单击该按钮时 我的数据库应导出到指定的路径 我使用了以下代码 但它不起作用 Runtime runti
  • 如果列有多个逗号分隔值,如何过滤 mysql 数据?

    我想问如果检查条件以查找具有多个逗号分隔值的列 如何过滤 mysql 数据 我给你举个例子 我有下表说 tbitems id item names item types item features 1 item 1 8 6 10 5 4 9
  • SQL统计高于和低于平均分的学生人数

    我在下面有一个示例表 我试图获取高于平均分数的学生人数和低于平均分数的学生人数 name subject classroom classarm session first term score first term grade std1 m
  • MySQL用户创建的临时表已满

    我使用内存引擎创建了一个临时表 如下所示 CREATE TEMPORARY TABLE IF NOT EXISTS some text id INT DEFAULT 0 string varchar 400 DEFAULT engine m
  • Codeigniter 加入多个条件

    我正在使用 Codeigniter Active Records 课程 我想加入我的users与我的桌子clients表 这样我就可以显示用户的 真实 姓名 而不仅仅是他们的 ID 这是什么clients表看起来像 示例 列 a 1 a 2
  • Preg_replace() 删除除查询结尾之外的所有内容

    首先 为我糟糕的英语感到抱歉 我有这样的疑问 SELECT t1 SELECT COUNT FROM table a t2 WHERE t1 id t2 id c AND t2 status 1 AS aula FROM table c t
  • 使用唯一索引删除重复项

    我在两个表字段 A B C D 之间插入 相信我已经在 A B C D 上创建了唯一索引以防止重复 然而我以某种方式简单地对这些做了一个正常的索引 因此插入了重复项 这是2000万条记录的表 如果我将现有索引从普通索引更改为唯一索引 或者只
  • 通过货币换算获取每种产品类型的最低价格

    我想选择每种产品类型中最便宜的 包括运费 价格转换为当地货币 最便宜 产品 价格 产品 运费 seller to aud 我的数据库有如下表 PRODUCTS SELLERS id type id seller id price shipp
  • MySQL 服务器未启动

    当我做 mysql u root p并输入my password这就是我得到的 错误 2002 HY000 无法通过套接字 var run mysqld mysqld sock 连接到本地 MySQL 服务器 2 所以我输入 systemc
  • PHP 选择后立即删除

    我有一个 PHP 服务器脚本 它从 MySQL 数据库中选择一些数据 一旦我将 mysql query 和 mysql fetch assoc 的结果存储在我自己的局部变量中 我就想删除我刚刚选择的行 这种方法的问题在于 PHP 似乎对我的
  • MySQL 存储过程将值分配给 select 语句中的多个变量

    这是我的存储过程 我在为声明的变量赋值时遇到问题 当我执行它时 插入和更新命令工作正常 但声明变量的值保持为 0 但我在数据库中有一些价值 我怎样才能正确地做到这一点 BEGIN DECLARE PaidFee INT DEFAULT 0
  • MYSQL - 查找最近的前一天

    我可以以某种方式 不使用 PHP 找到一周中最近的前一天日期吗 Like 最近的上一个星期二的日期是哪一天 CURDATE INTERVAL WEEKDAY CURDATE wday IF WEEKDAY CURDATE gt wday 0
  • mysql-如何向列申请补助?

    用户名 撤销对数据库的选择 Person I set GRANT SELECT id ON database Person TO username localhost 不是工作 gt SELECT secret FROM Person Go

随机推荐