This feature is asked for by many users, but until it is done, this program can be used instead. it is a small program, which also demonstrates few MySQL++ features. #include <sqlplus.hh>
#define MY_DATABASE "telcent"
#define MY_TABLE "nazivi"
#define MY_HOST "localhost"
#define MY_USER "root"
#define MY_PASSWORD ""
#define MY_FIELD "naziv"
#define MY_QUERY "SELECT URL from my_table as t1, my_table as t2 where t1.field = t2.field"
int main (void) {
Connection con(use_exceptions);
try {
ostrstream strbuf; unsigned int i=0;
con.real_connect (MY_DATABASE,MY_HOST,MY_USER,MY_PASSWORD,3306,(int)0,60,NULL);
Query query = con.query(); query << MY_QUERY;
ResUse res = query.use(); Row row;
strbuf << "delete from " << MY_TABLE << " where " << MY_FIELD << " in (";
// for UPDATE just replace the above DELETE FROM with UPDATE statement
for(;row=res.fetch_row();i++) strbuf << row[0] << ","; if (!i) return 0;
string output(strbuf.str()); output.erase(output.size()-1,1); output += ")";
query.exec((const string&)output); // cout << output << endl;
return 0;
} catch (BadQuery er) {
cerr << "Error: " << er.error << " " << con.errnum() << endl;
return -1;
}
}
#define MY_DATABASE "telcent"
#define MY_TABLE "nazivi"
#define MY_HOST "localhost"
#define MY_USER "root"
#define MY_PASSWORD ""
#define MY_FIELD "naziv"
#define MY_QUERY "SELECT URL from my_table as t1, my_table as t2 where t1.field = t2.field"
int main (void) {
Connection con(use_exceptions);
try {
ostrstream strbuf; unsigned int i=0;
con.real_connect (MY_DATABASE,MY_HOST,MY_USER,MY_PASSWORD,3306,(int)0,60,NULL);
Query query = con.query(); query << MY_QUERY;
ResUse res = query.use(); Row row;
strbuf << "delete from " << MY_TABLE << " where " << MY_FIELD << " in (";
// for UPDATE just replace the above DELETE FROM with UPDATE statement
for(;row=res.fetch_row();i++) strbuf << row[0] << ","; if (!i) return 0;
string output(strbuf.str()); output.erase(output.size()-1,1); output += ")";
query.exec((const string&)output); // cout << output << endl;
return 0;
} catch (BadQuery er) {
cerr << "Error: " << er.error << " " << con.errnum() << endl;
return -1;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <lcnet.h>
#include <mysql.h>using namespace std;int main()
{
// crando variables globales
MYSQL coneccion;
// construyendo pagina
cout << "Content-type: text/html\n\n";
cout << "<html>" << endl;
// inicializando y conectando
if ( mysql_init(&coneccion) == NULL)
cout << "error inicializando" << endl;
if ( mysql_real_connect(&coneccion,"localhost","USUARIO","PASSWORD","mibase",0,NULL,0) == NULL )cout << "error conectando" <<endl;// construyendo insert query char q[200], nombre[30];strcpy(nombre, getstring("NOMBRE").c_str() );strcpy(q,"INSERT INTO mitabla (nombre,edad,estatura) VALUES ( \"" );strcat(q,nombre);strcat(q," \", ");char temp[20];strcpy(temp, getstring("EDAD").c_str() );strcat(q,temp);strcat(q,",");strcpy(temp, getstring("ESTATURA").c_str() );strcat(q,temp);strcat(q,") ");cout << q << endl;if ( mysql_query(&coneccion,q) !=0){cout << "error en insert " <<endl;} else cout << "registro insertado " << endl; mysql_close(&coneccion);cout << "</html>" << endl;exit(0);}
我现在用的是MySql4.0
int mysql_query(MYSQL *mysql, const char *query) ;把你想要的操作(查询,删除,修改,添加……)写成相应的sql语句就可以了。
mysqlquery两个函数都可以。