如何从数据库读取数据

问题描述:

我使用phpmyadmin sql作为数据库

有一个名称为users的表,我的数据库名称为vcs

表中有4行

我想使用c ++从数据库中读取它

i m using phpmyadmin sql as database

there is a table with name users and my database name is vcs

there are 4 rows in table

i want to read it from database using c++

#include <mysql.h>
#include <stdio.h>
#include <iostream>

MYSQL mysql,*connection;
MYSQL_RES *result;
MYSQL_ROW row;

char * ip = (char*)"192.168.49.131";
char * usr = (char*)"root";
char * pass = (char*)"admin";
char * db = (char*)"vcs";

unsigned fields;
using namespace std;
int main ()
{
	mysql_init(&mysql);

	connection = mysql_real_connect(&mysql, ip, usr, pass, db, 0, NULL, 0);

	if (connection==NULL)
	{
		std::cout << mysql_error(&mysql)<< std::endl;
	}

	else
	{
		if(mysql_query(&mysql, "INSERT INTO `vcs`.`users`  (`id`, `fname`, `sname`) VALUES (5, 'Rana', 'sameer')"));

		else
		{
			result = mysql_store_result(&mysql);
			fields = mysql_num_fields(result);

			while((row = mysql_fetch_row (result)))
			{
				unsigned long *length;
				length = mysql_fetch_lengths(result);

				for(int i=0; i<fields; i++)
				{
					cout <<  length[i], row[i] , row[i]  ;
					cout << "NULL";
				}
				cout << endl;

			}
		}
		//if (query_state !=0)
		//{
		//cout << mysql_error(connection) << endl;
		//return 1;
		//}
	}


}



我正在使用此代码

但我看不到数据

请帮助我如何从数据库中读取所有行

我在c ++中是新的



i m using this code

but i m not be able to see data

plz help me how can i read all rows from database

i m new in c++

我还没有使用MySQL,但是在其他任何RDBS中,您都使用SELECT读取数据,并使用INSERT写入数据.
希望这会有所帮助,
Pablo.
I haven''t used MySQL, but in any other RDBS you use SELECT to read data, and INSERT to write data.
Hope this helps,
Pablo.


我不太确定您要做什么,但是,这是从mysql中的表中查询所有行的方法.

选择* FROM TableName

您可能还需要查看是否包含INSERT查询.似乎有一个语法错误,那里有一个;在if语句之后.

希望对您有所帮助.
I am not quite sure what you are trying to do, but, here is how you query all rows from a table in mysql.

SELECT * FROM TableName

You also might want to look at your if else containing the INSERT query. There looks to be a syntax error in there where there is a ; after your if statement.

I hope this helps.


您没有告诉我们您的实际问题,只是说您看不到数据.您可能有连接问题,也可能有查询问题.

您需要了解的一些问题:
1.您没有使用 phpmyadmin sql 作为数据库.您正在使用MySQL作为数据库.

2.您的cout将仅显示第一个变量

3.声明名称空间时,实际上不需要使用std :: cout,仅使用cout即可.

4.他们定义和连接mysql的方式没有错,但不合适.请遵循以下代码:

you didn''t tell us your actual problem, you just said you are not able to see data. You can have connection problem, you can have Query problem.

Few Issues you need to know:
1. You are not using phpmyadmin sql as your database. you are using MySQL as your database.

2. Your cout will display only the first variable

3. When you declare name space you actually dont need to use std::cout, cout only is enough.

4. They way you defined and connected mysql is not wrong but not appropriate. follow the code below:

 MYSQL *conn;

if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      //error message
      exit(1);
   }



5.用户和主机权限问题.确保您的mysql具有允许的主机和用户.也就是说,您的用户可能没有权限通过ip:192.168.49.131与mysql数据库连接.确保权限



5. user and host permission issue. make sure your mysql has a permitted host and user. i.e., your user may not have permission to connect with mysql database from ip: 192.168.49.131. ensure permission