C语言中如何将变量插入mySQL数据库和mySQL?C语言封装变量的问题如何解决?

C语言中如何将变量插入mySQL数据库和mySQL?C语言封装变量的问题如何解决?,第1张

假如用户输入的姓名和年龄值分别保存在变量name,age中:

char sql_insert[200]

sprintf(sql_insert, "INSERT INTO table values('%s','%d')", name, age)

mysql_query(&mysql_conn, sql_insert)

上述语句在执行完sprintf语句后,sql_insert中保存的是INSERT查询语句字符串,sql_insert作为mysql_query()语句的参数即可实现正确的插入。

1.需要用到mysql c api

2.步骤通常是:连接数据库,执行查询,异常处理这些。

3.你需要对api 进行一些了解,然后合理调用。

参考代码:

//连接代码:

/*

* connect1.c - connect to and disconnect from MySQL server

*/

#include <my_global.h>

#include <my_sys.h>

#include <mysql.h>

static char *opt_host_name = NULL /* server host (default=localhost) */

static char *opt_user_name = NULL /* username (default=login name) */

static char *opt_password = NULL /* password (default=none) */

static unsigned int opt_port_num = 0 /* port number (use built-in value) */

static char *opt_socket_name = NULL /* socket name (use built-in value) */

static char *opt_db_name = NULL /* database name (default=none) */

static unsigned int opt_flags = 0 /* connection flags (none) */

static MYSQL *conn/* pointer to connection handler */

int

main (int argc, char *argv[])

{

/* initialize connection handler */

conn = mysql_init (NULL)

if (conn == NULL)

{

fprintf (stderr, "mysql_init() failed (probably out of memory)\n")

exit (1)

}

/* connect to server */

if (mysql_real_connect (conn, opt_host_name, opt_user_name, opt_password,

opt_db_name, opt_port_num, opt_socket_name, opt_flags) == NULL)

{

fprintf (stderr, "mysql_real_connect() failed\n")

mysql_close (conn)

exit (1)

}

/* disconnect from server */

mysql_close (conn)

exit (0)

}

//处理sql 语句的函数:

void

process_statement (MYSQL *conn, char *stmt_str)

{

MYSQL_RES *res_set

if (mysql_query (conn, stmt_str) != 0) /* the statement failed */

{

print_error (conn, "Could not execute statement")

return

}

/* the statement succeededdetermine whether it returned data */

res_set = mysql_store_result (conn)

if (res_set) /* a result set was returned */

{

/* process rows and then free the result set */

process_result_set (conn, res_set)

mysql_free_result (res_set)

}

else /* no result set was returned */

{

/*

* does the lack of a result set mean that the statement didn't

* return one, or that it should have but an error occurred?

*/

if (mysql_field_count (conn) == 0)

{

/*

* statement generated no result set (it was not a SELECT,

* SHOW, DESCRIBE, etc.)just report rows-affected value.

*/

391

Processing SQL Statements

printf ("%lu rows affected\n",

(unsigned long) mysql_affected_rows (conn))

}

else /* an error occurred */

{

print_error (conn, "Could not retrieve result set")

}

}

}

//以上提供的是常规的处理,你需要根据实际情况完善。

//同时调用了一些mysql 的api函数。

ref:

MySQL

(Fourth Edition)

Paul DuBois

(1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序

下载地址为:

http://dev.mysql.com/downloads/connector/net/6.0.html

我下载的版本为: mysql-connector-net-6.3.8.msi

下载地址如下url:

http://dev.mysql.com/downloads/mirror.php?id=405442

(2)安装mysql-connector-net

然后直接在Windows *** 作系统安装 mysql-connector-net-6.3.8.msi

默认是安装在C盘:

C:\Program Files\MySQL\MySQL Connector Net 6.3.8\Assemblies

v2.0

v4.0

安装完后我选择的是v2.0版本的

然后在应用工程中引用组件MySQL.Data.dll

(3)封装数据库访问组件DbConnectionMySQL

/// <summary>

/// MySQL数据库

/// 版本 mysql-connector-net-6.3.8.msi

/// vp:hsg

/// create date:2012-02-28

/// </summary>

[Serializable]

public class DbConnectionMySQL : DbConnectionWrapper

{

public DbConnectionMySQL(string pConnectionString)

: base(pConnectionString)

{

this.m_dbconn = new MySqlConnection(pConnectionString)

this.m_DbConnState = DbConnState.Free

}

//--

public override DbDataAdapter GetDbDataAdapter()

{

return new MySqlDataAdapter()

}

public override DbDataAdapter GetDbDataAdapter(DbCommand dbCommand)

{

return new MySqlDataAdapter(dbCommand as MySqlCommand)

}

public override DbCommand GetDbCommand()

{

return new MySqlCommand()

}

public override DbConnection GetDbConnection()

{

return new MySqlConnection()

}

public override DbCommandBuilder GetDbCommandBuilder()

{

return new MySqlCommandBuilder()

}

public override DataProviderType GetCurrentDataProviderType()

{

return DataProviderType.Sql

}

public override bool IsExistsTable(string TableName, string UserName)

{

#region information

bool rbc = false //TABLES表中去查询 table_name

string dSql = "select * from TABLES where table_name='" + TableName + "'"

DataSet ds = this.ExecuteDataSet(dSql)

if (ds != null)

{

if (ds.Tables[0].Rows.Count >0)

{

rbc = true

}

else

{

rbc = false

}

}

else

{

rbc = false

}

return rbc

#endregion

}

public override bool IsExistsField(string FieldName, string TableName)

{

#region information

bool rbc = false

string dSql = ""

dSql = "select * from " + TableName + " where 1<>1"

DataSet ds = this.ExecuteDataSet(dSql)

if (ds != null)

{

DataTable dt = ds.Tables[0]

for (int j = 0j <dt.Columns.Countj++)

{

if (dt.Columns[j].ColumnName.ToString().ToUpper() == FieldName.ToString().ToUpper())

{

rbc = true

goto Return_End

}

}

dt.Dispose()

dt = null

}

ds.Dispose()

ds = null

Return_End:

return rbc

#endregion

}

public override char ParameterChar

{

get

{

return ':' //SQLite的参数符号为:

}

}

public override DbParameter CreateParameter(string name, object value)

{

return new MySqlParameter(name, value)

}

public override DbParameter CreateParameter(string name)

{

DbParameter dbp = new MySqlParameter()

dbp.ParameterName = name

return dbp

}

public override DbParameter CreateParameter(string name, DbType dbtype, object value)

{

DbParameter dbp = new MySqlParameter()

dbp.ParameterName = name

dbp.Value = value

dbp.DbType = dbtype

return dbp

}

public override DbParameter CreateParameter(string name, DbType dbtype, int size, object value)

{

DbParameter dbp = new MySqlParameter()

dbp.ParameterName = name

dbp.Value = value

dbp.DbType = dbtype

dbp.Size = size

return dbp

}

}

(4)客户端访问测试开发实例

public void TestCShape_MySQL()

{

string constr = "server=localhostUser Id=rootpassword=rootDatabase=xp_users"

DbConnectionWrapper dbw = new DbConnectionMySQL(constr)

bool rbc=dbw.TestConnection()

this.Context.Response.Write(rbc)

string x = ""

//删除语句

x = "delete from xp_users"

if (dbw.ExecuteQuery(x) >0)

{

this.Context.Response.Write("删除语句成功!下面是SQL语句<br>" + x)

}

//插入语句

x = "insert into xp_users(gid,uid,uname,sex,email,pwd) values('"

x += "1','hsg77','何XXX',1,'hsg77@163.com','1')"

if (dbw.ExecuteQuery(x) >0)

{

this.Context.Response.Write("插入语句成功!下面是SQL语句<br>"+x)

}

//查询语句

DataTable dt = dbw.ExecuteDataTable("select * from xp_users")

if (dt != null &&dt.Rows.Count >0)

{

this.Context.Response.Write("<br>用户数:"+dt.Rows.Count)

}

if (dt != null)

{

dt.Dispose()

dt = null

}

dbw.Dispose()

dbw = null

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6133273.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-15
下一篇 2023-03-15

发表评论

登录后才能评论

评论列表(0条)

保存