Zmienne w zapytaniu MYSQL

0

Witam, od kilku dni szukam sposobu na wstawienie zmiennych w zapytaniu mysql.
korzystam z Visual C++ 2010.
Oto mój prosty program łączący się z Bazą Danych, wszystko działa dane są pobieranie z bazy i wyświetlane, lecz problem pojawia się gdy do zapytania próbuje dodać zmienną...
Chodzi mi oto:

        int x = 1;
	query_state = mysql_query(&mysql, SELECT * FROM nns_user WHERE user_id = "+ x);

ale wyskakuje mi wtedy błąd:

error C2664: 'mysql_query' : cannot convert parameter 2 from 'System::String ^' to 'const char *'

dlatego użyłem innego sposobu przedstawionego niżej ale również nie działa...
Szukałem, sprawdzałem wszystkiego nie mam pojęcia w czym tkwi problem.
Z góry dziękuje za pomoc ;-)

program wygląda tak:

user image

#pragma once
#include <iostream>
#include <winsock.h> 
#include <mysql/mysql.h> 
#include <stdio.h>
#include <string.h>

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

namespace MySQLProgram {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::TextBox^  TextBox;
	protected: 
	private: System::Windows::Forms::Button^  Aktualizuj;
	private: System::Windows::Forms::TextBox^  TextBox1;
	private: System::Windows::Forms::Label^  label1;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->TextBox = (gcnew System::Windows::Forms::TextBox());
			this->Aktualizuj = (gcnew System::Windows::Forms::Button());
			this->TextBox1 = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->SuspendLayout();
			// 
			// TextBox
			// 
			this->TextBox->Location = System::Drawing::Point(92, 60);
			this->TextBox->Multiline = true;
			this->TextBox->Name = L"TextBox";
			this->TextBox->Size = System::Drawing::Size(360, 133);
			this->TextBox->TabIndex = 0;
			// 
			// Aktualizuj
			// 
			this->Aktualizuj->Location = System::Drawing::Point(195, 209);
			this->Aktualizuj->Name = L"Aktualizuj";
			this->Aktualizuj->Size = System::Drawing::Size(161, 32);
			this->Aktualizuj->TabIndex = 1;
			this->Aktualizuj->Text = L"Aktualizuj";
			this->Aktualizuj->UseVisualStyleBackColor = true;
			this->Aktualizuj->Click += gcnew System::EventHandler(this, &Form1::Aktualizuj_Click);
			// 
			// TextBox1
			// 
			this->TextBox1->Location = System::Drawing::Point(234, 12);
			this->TextBox1->Name = L"TextBox1";
			this->TextBox1->Size = System::Drawing::Size(48, 20);
			this->TextBox1->TabIndex = 2;
			this->TextBox1->Text = L"1";
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(192, 15);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(18, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"ID";
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(542, 280);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->TextBox1);
			this->Controls->Add(this->Aktualizuj);
			this->Controls->Add(this->TextBox);
			this->Name = L"Form1";
			this->Text = L"Program";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
			 {
				 	mysql_init(&mysql);
					connection = mysql_real_connect(&mysql,"127.0.0.1","root","","datex",0,0,0);
					if(connection == NULL)
					{
						this->TextBox->Text = L"Error: " + gcnew String(mysql_error(&mysql));
						this->Aktualizuj->Enabled = false;
					}
			 }

	private: System::Void Aktualizuj_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				char sQuery[256]; 
				const char x= 1;
				sprintf_s(sQuery, "SELECT * FROM nns_user WHERE user_id = '%s'", x);


				 	query_state = mysql_query(&mysql, sQuery);
					result = mysql_store_result(connection);
				    while ( ( row = mysql_fetch_row(result)) != NULL )
					{
					this->TextBox->Text = this->TextBox->Text + L"\r\n Imie: " + gcnew String(row[1]) + L" e-mail: " + gcnew String(row[9]);				
					}
			 }

	};
}

0

Twój kod:
int x = 1;
query_state = mysql_query(&mysql, "SELECT * FROM nns_user WHERE user_id = "+ x);

zamienilem na:
int x = 1;
String^ s = gcnew String("SELECT * FROM nns_user WHERE user_id = ");
s += x;
query_state = mysql_query(&mysql, s); //ponieważ próbowalem na testowym programie i nie mam w nim MySQL, to tą linię należy sprawdzić. Ale samo dodanie zmiennej x do stringa na pewno działa.

0

Ten sam błąd:

 error C2664: 'mysql_query' : cannot convert parameter 2 from 'System::String ^' to 'const char *'

:(

Spróbowałem inaczej:

int x = 1;
const char* s = "SELECT * FROM nns_user WHERE user_id =";
s += x;
query_state = mysql_query(&mysql, s);

Program skompilował się ładnie bez błędów ale po odpaleniu wyskoczył taki błąd:

user image

0

int x = 1;
String^ s = gcnew String("SELECT * FROM nns_user WHERE user_id = ");
s += x;
const char* xx = (const char*)Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer( ); //wygooglałem przed chwilą

query_state = mysql_query(&mysql, xx);

0
Tomek napisał(a)

int x = 1;
String^ s = gcnew String("SELECT * FROM nns_user WHERE user_id = ");
s += x;
const char* xx = (const char*)Runtime::InteropServices::Marshal::StringToHGlobalAnsi(s).ToPointer( ); //wygooglałem przed chwilą

query_state = mysql_query(&mysql, xx);

Śmiga, dzięki ;)

1 użytkowników online, w tym zalogowanych: 0, gości: 1