如果您对AccessMySQLfromC感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于AccessMySQLfromC的详细内容,我们还将为您解答用C访问MySQL数据库的
如果您对Access MySQL from C 感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于Access MySQL from C 的详细内容,我们还将为您解答用 C 访问 MySQL 数据库的相关问题,并且为您提供关于.net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式、access mysql mssql 随机 10条数据的sql语句 原创、Access 数据导入mysql_MySQL、ACCESS数据库向MySQL快速迁移小程序(一)_MySQL的有价值信息。
本文目录一览:- Access MySQL from C (用 C 访问 MySQL 数据库)(c语言访问mysql)
- .net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式
- access mysql mssql 随机 10条数据的sql语句 原创
- Access 数据导入mysql_MySQL
- ACCESS数据库向MySQL快速迁移小程序(一)_MySQL
Access MySQL from C (用 C 访问 MySQL 数据库)(c语言访问mysql)
This blog is mainly a collection of study notes and some simple tryout examples. For more details, refer to "Beginning Linux Programming", Chapter 8.
Most Commonly Used APIs for Accessing MySQL:
MYSQL *mysql_init(MYSQL *);
MYSQL *mysql_real_connect(MYSQL *connection,
const char *server_host,
const char *sql_user_name,
const char *sql_password,
const char *db_name,
unsigned int port_number,
const char *unix_socket_name,
unsigned int flags);
void mysql_close(MYSQL *connection);
int mysql_options(MYSQL *connection, enum option_to_set,
const char *argument);
int mysql_query(MYSQL *connection, const char *query);
my_ulonglong mysql_affected_rows(MYSQL *connection);
unsigned int mysql_errno(MYSQL *connection);
char *mysql_error(MYSQL *connection);
MYSQL_RES *mysql_store_result(MYSQL *connection);
my_ulonglong mysql_num_rows(MYSQL_RES *result);
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);
MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);
MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);
void mysql_free_result(MYSQL_RES *result);
unsigned int mysql_field_count(MYSQL *connection);
MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result);
char *mysql_get_client_info(void);
char *mysql_get_host_info(MYSQL *connection);
char *mysql_get_server_info(MYSQL *connection);
char *mysql_info(MYSQL *connection);
int mysql_select_db(MYSQL *connection, const char *dbname);
int mysql_shutdown(MYSQL *connection, enum mysql_enum_shutdown_level);
Example1: how to connect to a mysql server
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
int main(int argc, char *argv[])
{
MYSQL *conn_ptr;
conn_ptr = mysql_init(NULL);
if (!conn_ptr)
{
fprintf(stderr, "mysql_init failed\n");
exit(EXIT_FAILURE);
}
conn_ptr = mysql_real_connect(conn_ptr, "localhost", "chenqi", "helloworld",
"test", 0, NULL, 0);
if (conn_ptr)
{
printf("Connection Success \n");
}
else
{
printf("Connection failed \n");
}
mysql_close(conn_ptr);
exit(EXIT_SUCCESS);
}
gcc -I/usr/include/mysql connect1.c -L/usr/lib/mysql -lmysqlclient -o connect1
Example2: how to handle errors
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
int main(int argc, char *argv[])
{
MYSQL conn;
mysql_init(&conn);
if (mysql_real_connect(&conn, "localhost", "chenqi",
"i do not know", "test", 0, NULL, 0))
{
printf("Connection Success \n");
mysql_close(&conn);
}
else
{
fprintf(stderr, "Connection Failed \n");
if (mysql_errno(&conn))
{
fprintf(stderr, "Connection error %d: %s \n",
mysql_errno(&conn), mysql_error(&conn));
}
}
exit(EXIT_SUCCESS);
}
result:
chenqi@chenqi-laptop ~/MyPro/Database/access_with_c $ ./connect2
Connection Failed
Connection error 1045: Access denied for user ''chenqi''@''localhost'' (using password: YES)
Example3: how to insert and update
/* insert a row into the table children */
ret = mysql_query(&conn, "insert into children(fname, age) values(''James'', 23)");
if (!ret)
{
printf("Inserted %lu rows \n",
(unsigned long)mysql_affected_rows(&conn));
}
else
{
fprintf(stderr, "Insert error %d: %s \n",
mysql_errno(&conn), mysql_error(&conn));
}
/* update a row in the table children */
ret = mysql_query(&conn, "update children set age = 24 where fname = ''James''");
if (!ret)
{
printf("Update %lu rows \n",
(unsigned long)mysql_affected_rows(&conn));
}
else
{
fprintf(stderr, "Update error %d: %s \n",
mysql_errno(&conn), mysql_error(&conn));
}
result:
chenqi@chenqi-laptop ~/MyPro/Database/access_with_c $ ./insert-update
Connection Success
Inserted 1 rows
Update 2 rows
Example4: how to retrieve data into C application
Step1: Issue the query (mysql_query)
Step2: Retrieve the data (mysql_store_result, mysql_use_result)
Step3: Process the data (mysql_fetch_row)
Step4: Tidy up if necessary (mysql_free_result)
For a large data set, mysql_use_result should be considered, because it uses less storage.
MySQL, like other SQL databases, gives back two sorts of data:
1. The retrieved information from the table, namely the column data
2. Data about the data, so-called metadata, such as column types and names
#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
void display_row()
{
unsigned int field_count = 0;
while(field_count < mysql_field_count(&my_connection))
{
if (sqlrow[field_count])
printf("%12s", sqlrow[field_count]);
else
printf("%12s", "NULL");
field_count++;
}
printf("\n");
}
/* show metadata of each column */
void display_header()
{
MYSQL_FIELD *field_ptr;
printf("Column Details:\n");
while((field_ptr = mysql_fetch_field(res_ptr)) != NULL)
{
printf("\t Name: %s\n", field_ptr->name);
printf("\t Type: ");
if (IS_NUM(field_ptr->type))
{
printf("Numeric Field\n");
}
else
{
switch(field_ptr->type)
{
case FIELD_TYPE_VAR_STRING:
printf("VARCHAR\n");
break;
case FIELD_TYPE_LONG:
printf("LONG\n");
break;
default:
printf("Type is %d, check in mysql_com.h\n", field_ptr->type);
}
} /* else */
printf("\t Max_width %ld\n", field_ptr->max_length);
if (field_ptr->flags & AUTO_INCREMENT_FLAG)
{
printf("\t Auto increments\n");
}
printf("\n");
} /* while */
}
int main(int argc, char *argv[])
{
int ret;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost", "chenqi",
"helloworld", "test", 0, NULL, 0))
{
printf("Conncetion Success\n");
ret = mysql_query(&my_connection, "select * from children");
if (ret) /* error */
{
printf("select error: %s\n", mysql_error(&my_connection));
}
else /* ok */
{
res_ptr = mysql_store_result(&my_connection); /* mysql_use_result for an alternative */
if (res_ptr)
{
printf("Retrieved %lu rows \n", (unsigned long)mysql_num_rows(res_ptr));
display_header();
while (sqlrow = mysql_fetch_row(res_ptr))
{
/// printf("Fetching data ... \n");
display_row();
}
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Retrieve error: %s\n", mysql_error(&my_connection));
}
mysql_free_result(res_ptr); /* res_ptr != NULL */
}
}
mysql_close(&my_connection);
}
else
{
fprintf(stderr, "Connection Failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection), mysql_error(&my_connection));
}
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
Result:
chenqi@chenqi-laptop ~/MyPro/Database/access_with_c $ ./select1
Conncetion Success
Retrieved 12 rows
Column Details:
Name: childno
Type: Numeric Field
Max_width 2
Auto increments
Name: fname
Type: VARCHAR
Max_width 8
Name: age
Type: Numeric Field
Max_width 2
1 Jenny 21
2 Feby 25
3 Chandler 12
4 Monica 23
5 Rachel 21
6 Ross 2
7 Joy 11
8 Emma 11
9 Gavin 14
10 Andrew 21
12 James 24
13 Tom 13
.net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式
便签记录Mysql,Sql server,Sqlite,Access四种数据库的简单连接方式


//using MySql.Data.MySqlClient;
#region 执行简单SQL语句,使用MySQL查询
static string strConn = "server=.;database=Data20180608;uid=sa;pwd=123456;integrated Security=SSPI;persist Security info=false;";
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int MyExecuteSql(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(strConn))
{
using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataTable MyQuery(string SQLString)
{
using (MySqlConnection connection = new MySqlConnection(strConn))
{
DataSet ds = new DataSet();
try
{
connection.Open();
MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds.Tables[0];
}
}
#endregion


#region 执行简单SQL语句,使用SQL SERVER查询
static string strConn = "Data Source=.;database=Data20180608;User id=root;Password=123456;pooling=false;CharSet=utf8;port=3306;";
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(strConn))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataTable Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(strConn))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (System.Data.SqlClient.SqlException ex)
{
throw new Exception(ex.Message);
}
return ds.Tables[0];
}
}
#endregion


//using System.Data.OleDb;
public static string OtherPAth = "";
public static string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
public static string Sql = String.Empty;
#region 执行简单SQL语句,使用OleDb查询
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth ))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
throw e;
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataTable Query(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return ds.Tables[0];
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public static DataTable QueryDataName()
{
using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
{
DataTable ds = new DataTable();
try
{
connection.Open();
ds = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
return ds;
}
}
#endregion


public static string strConn = @"Data Source=" + @"F:\资料文档\20190227\CAD\0625AnPin\CadDLLcl\Data\Cad_try0626.db";
public static string Sql = String.Empty;
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSql(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
//事务
using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.Transaction = singleTrans;
int rows = cmd.ExecuteNonQuery();
singleTrans.Commit();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
singleTrans.Rollback();
throw e;
}
}
}
}
}
/// <summary>
/// 执行查询语句,返回DataTable
/// </summary>
/// <param name = "SQLString" > 查询语句 </ param >
/// < returns > DataSet </ returns >
public static DataTable Query(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
return ds.Tables[0];
}
catch (Exception ex)
{
connection.Close();
throw ex;
}
}
}
/// <summary>
/// 执行存储过程,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteProc(string procName, SQLiteParameter[] coll)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
//事务
using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (SQLiteCommand cmd = new SQLiteCommand(connection))
{
try
{
connection.Open();
for (int i = 0; i < coll.Length; i++)
{
cmd.Parameters.Add(coll[i]);
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procName;
cmd.Transaction = singleTrans;
int rows = cmd.ExecuteNonQuery();
singleTrans.Commit();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
singleTrans.Rollback();
throw e;
}
}
}
}
}
/// <summary>
/// 执行带参数的SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public static int ExecuteSqlPar(string sqlPar, SQLiteParameter[] coll)
{
using (SQLiteConnection connection = new SQLiteConnection(strConn))
{
//事务
using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
{
using (SQLiteCommand cmd = new SQLiteCommand(connection))
{
try
{
connection.Open();
for (int i = 0; i < coll.Length; i++)
{
cmd.Parameters.Add(coll[i]);
}
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlPar;
cmd.Transaction = singleTrans;
int rows = cmd.ExecuteNonQuery();
singleTrans.Commit();
return rows;
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
singleTrans.Rollback();
throw e;
}
}
}
}
}
access mysql mssql 随机 10条数据的sql语句 原创
access 随机 10条数据的方法select * from table order by rnd(id)
其中rnd(id)中的id为表中的自增长字段
access随机显示记录(不重复)解决方案
<%
''-------------------------数据库连接-----------------------
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" &_
"Data Source=" & Server.MapPath("data.mdb")
objConn.Open
''-------------------------数据库连接-----------------------
''-------------------------检索数据-----------------------
strSQL = "SELECT id,DataColumn FROM DataTable"
''Sql语句,检索数据库
Set objRS = Server.CreateObject("ADODB.Recordset")
''创建记录集
objRS.Open strSQL, objConn, 1, 1
''执行检索
Count=objRS.RecordCount
''得到记录总数
Item=4
''显示记录数
''-------------------------检索数据-----------------------
''-------------------------------------------------------------------------------
redim a(Item, 2),t(Count)
''定义2数组,数组a用来储存记录,数组t用来删选记录
''---------------------------------------
''初始数组数值,目的为了插入数据以后和此值做比较
for each j in t
j=0
next
''---------------------------------------
''---------------------------------------
'' 随机抽取记录号
Randomize timer ''初始化随机数生成器
for j=1 to Item
k=int(rnd*Count+1) ''从总数里面随机取一条记录
do while t(k)<>0 ''判断是否记录是否已经在数组中
k=int(rnd*Item+1)
loop
t(k)=1 ''第k条记录被选中
next
''--------------------------------------
j=1:i=1''定义下标
''--------------------------------------
'' 循环选取数据集objRS中的部分记录存放到数组中
Do While Not objRS.Eof
if t(j)=1 then
a(i,1)=objRS("id") ''记录id
a(i,2)=objRS("DataColumn") ''记录内容
i=i+1
end if
j=j+1
objRS.MoveNext
Loop
''--------------------------------------
''-------------------------------------------------------------------------------
''----------------------------显示内容--------------------
for i=1 to Item
Response.write "序号"&a(i,1)&"<br>"
Response.write "内容"&a(i,2)&"<p>"
next
''----------------------------显示内容--------------------
''---------------------------
''释放资源
objRs.Close
set objRs=nothing
objConn.Close
set objConn=nothing
''---------------------------
%>