使用C连接Mysql
2007-11-13 11:03:58 来源:WEB开发网char szTargetDSN[] = "test";
char szSqlText[500]="";
char aszFlds[ 25 ][ 25 ];
MYSQL * myData ;
MYSQL_RES * res ;
MYSQL_FIELD * fd ;
MYSQL_ROW row ;
int i,j,k;
BOOL bCreate = TRUE;
if ( (myData = mysql_init((MYSQL*) 0))
//初始化数据结构
&& mysql_real_connect( myData, NULL,
//连接数据库
"root", " your_password ", szTargetDSN,
MYSQL_PORT, NULL, 0 ) )
{
if(bCreate)
{
sprintf(szSqlText, //构造SQL语句
"create table mytable "
//新建一张表
"(time datetime, s1 char(6), "
"s2 char(11), s3 int, s4 int)");
if (mysql_query( myData, szSqlText))
//执行SQL语句
{//执行SQL语句出错
ErrLog( "Can't create table") ;
mysql_close( myData ) ;
return FALSE ;
}
}
sprintf(szSqlText,
"insert into mytable "
//向表中插入数据
"values('2000-3-10 21:01:30',"
//注意时间的格式
"'Test','MySQLTest',2000,3)");
if (mysql_query( myData, szSqlText))
{//执行SQL语句出错
ErrLog( "Can't insert data to table") ;
mysql_close( myData ) ;
return FALSE ;
}
sprintf(szSqlText, "select * from mytable ");
更多精彩
赞助商链接