澳门新葡萄京娱乐网站-澳门新葡萄京888官网-所有平台

热门关键词: 澳门新葡萄京娱乐网站,澳门新葡萄京888官网

澳门新葡萄京888官网动用Mysql的详实步骤及种种常

作者: MySQL数据库  发布:2019-12-11

这段日子一直在读书C++下使用Mysql的方法及中间各个的难点,也看了成都百货上千Mysql的API函数,当然自身看的依旧很功底的。其实对于每种数据库的操作,基本的主意都以丰裕相像的,许多都是connect,select,update,delete以至insert那多少个操作。接下来作者就将那多少个步骤以代码的措施表现给大家,相信应该能对大家对mysql的编制程序起到四个幼功模板的效用,方便咱们随后对mysql数据库的问询和上学。

开发包:appserv-win32-2.5.10

1.mysql的初叶化initConnection:

服务器:Apache2.2

1)mysql_init(卡塔尔:开端化数据库

数据库:phpMyAdmin

2)mysql_real_connect(卡塔尔(不引入用Mysql_connect(卡塔尔(قطر‎卡塔尔国:连接数据库

语言:php5,java

详尽代码如下:

平台:windows 10

 1 bool dataBase::initConnection() {
 2     try {
 3         if (mysql_init(&m_sqlCon) == NULL) {
 4             std::cout << "初始化数据库失败" << std::endl;
 5             return false;
 6         }
 7         // localhost:服务器 root为账号密码 test为数据库名 3306为端口    
 8         if (!mysql_real_connect(&m_sqlCon, host, user, pass,databaseName, port, NULL, 0))
 9         {
10             std::cout << "数据库连接失败" << std::endl;
11             return false;
12         }
13         std::cout << "数据库连接成功" << std::endl;
14         return true;
15     }
16     catch (...)
17     {
18         std::cout << "出错啦" << std::endl;
19         return false;
20     }
21 }

java驱动:mysql-connector-java-5.1.37

2.mysql的查询select:

编写多少个PHP脚本语言,连接收phpMyAdmin数据库的test库

1)mysql_query(卡塔尔:实行查询语句

编写制定二个java web服务端,连接收phpMyAdmin数据库的test库

2)mysql_store_result():拿到查询结果

mysql.php

3)mysql_fetch_field()以及mysql_fetch_row(卡塔尔(英语:State of Qatar):获得结果聚焦的从头到尾的经过

test.php测试

4)mysql_free_result()

java 连接格局

代码如下:

1.新建一个java project为mysqlTest

 1 void dataBase::user_query() {
 2     if (mysql_query(&m_sqlCon, "select * from user")) {
 3         std::cout << "查询失败" << std::endl;
 4         return;
 5     }
 6     std::cout << "查询成功" << std::endl;
 7     MYSQL_RES *result;
 8     result = mysql_store_result(&m_sqlCon);
 9     if (result) {
10         int row_num,col_num;
11         row_num = mysql_num_rows(result);
12         col_num = mysql_num_fields(result);
13         std::cout << "共有" << row_num << "条数据,以下为其详细内容:" << std::endl;
14         MYSQL_FIELD *fd;
15         while (fd = mysql_fetch_field(result)) {
16             std::cout << fd->name << "t";
17         }
18         std::cout << std::endl;
19         MYSQL_ROW sql_row;
20         while (sql_row = mysql_fetch_row(result)) {
21             for (int i = 0; i < col_num; i++) {
22                 if (sql_row[i] == NULL) std::cout << "NULLt";
23                 else std::cout << sql_row[i] << "t";
24             }
25             std::cout << std::endl;
26         }
27     }
28     if(result!=NULL)
29     mysql_free_result(result);
30 
31 }

2.加载JDBC驱动,mysql-connector-java-5.1.37

3.mysql的插入语句insert:

MySQLConnection.java

1)mysql_query(卡塔尔通过select推断是不是早就存在(能够轻松)

package com.mysqltest;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/* * **Mysql连接** * * 参数: * conn 连接 * url mysql数据库连接地址 * user 数据库登陆账号 * password 数据库登陆密码 * 方法: * conn 获取连接 */public class MySQLConnection { public static Connection conn = null; public static String driver = "com.mysql.jdbc.Driver"; public static String url = "jdbc:mysql://127.0.0.1:3306/post"; public static String user = "root"; public static String password = "123"; /* * 创建Mysql数据连接 第一步:加载驱动 Class.forName 第二步:创建连接 * DriverManager.getConnection; */ public Connection conn() { try { Class.forName; } catch (ClassNotFoundException e) { System.out.println; e.printStackTrace(); } try { conn = DriverManager.getConnection; } catch  { System.out.println; e.printStackTrace(); } return conn; }}

2)通过mysql_query()实行扦插

Work.java

代码如下:

package com.mysqltest;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;/* * mysql增删改查 */public class Work { /* * insert 增加 */ public static int insert() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句 int i = 0; String sql = "insert into user  values"; try { conns = connection.conn(); pst = conns.prepareStatement; pst.setString; pst.setString; i = pst.executeUpdate; conns.close(); } catch  { System.out.println; e.printStackTrace(); } return i; } /* * select 写入 */ public static void select() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句 ResultSet rs; // 获取返回结果 String sql = "select * from user"; try { conns = connection.conn(); pst = conns.prepareStatement; rs = pst.executeQuery;// 执行sql语句 System.out.println("---------------------------------------"); System.out.println; while  { System.out.println(rs.getString + " | " + rs.getString; } System.out.println("---------------------------------------"); conns.close; rs.close(); } catch  { System.out.println; e.printStackTrace(); } } /* * update 修改 */ public static int update() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句 int i = 0; String sql = "update user set password = ? where username = ?"; try { conns = connection.conn(); pst = conns.prepareStatement; pst.setString; pst.setString; i = pst.executeUpdate; conns.close(); } catch  { System.out.println; e.printStackTrace(); } return i; } /* * delete 删除 */ public static int delete() { MySQLConnection connection = new MySQLConnection(); Connection conns; // 获取连接 PreparedStatement pst; // 执行Sql语句 int i = 0; String sql = "delete from user where username = ?"; try { conns = connection.conn(); pst = conns.prepareStatement; pst.setString; i = pst.executeUpdate; conns.close(); } catch  { System.out.println; e.printStackTrace(); } return i; } /* * test */ public static void main { // System.out.println; // System.out.println; // System.out.println; }}
 1 void dataBase::user_insert(UserBody &body) {
 2     char select_user[255];
 3     char query[255];
 4     MYSQL_RES *result;
 5     sprintf(select_user, "select * from user where UserName='%s'", body.userName);
 6     if (mysql_query(&m_sqlCon, select_user)||!(result = mysql_store_result(&m_sqlCon))) {
 7         std::cout << "插入查询失败" << std::endl;
 8         return;
 9     }
10     if (mysql_num_rows(result)) {
11         std::cout << "用户已存在" << std::endl;
12         mysql_free_result(result);
13         return;
14     }
15     mysql_free_result(result);
16     sprintf(query, "insert into user(UserName,UserPass,ConnectionNum,UseFor) values('%s','%s',%d,%d)", body.userName, body.userPass, body.connection, body.type);
17     if (mysql_query(&m_sqlCon, query)) {
18         std::cout << "插入数据失败" << std::endl;
19         return;
20     }
21     std::cout << "插入数据成功,共插入:" <<mysql_affected_rows(&m_sqlCon)<<"行" << std::endl;
22 }

ps:php操作MySQL数据库中语句

4.mysql的校订语句update

大家平时用conn.php文件来确立与数据库的链接,然后在所需的文书中央银行使include 举办调用。那样有效防护对数据库属性的改观 而孳生别的有关文件对数码调用的谬误。

修正与丰盛的手续雷同,直接贴出代码就不做赘述了。

前不久来看二个conn.php文件,代码如下:

代码如下:

学学积存,搜集了PHP操作MYSQL的多少个根基函数:

 1 void dataBase::user_update(UserBody &body) {
 2     char query[255];
 3     char select_user[255];
 4     MYSQL_RES *result;
 5     sprintf(select_user, "select * from user where UserName='%s'", body.userName);
 6     if (mysql_query(&m_sqlCon, select_user) || !(result = mysql_store_result(&m_sqlCon))) {
 7         std::cout << "修改查询失败" << std::endl;
 8         return;
 9     }
10     if (mysql_num_rows(result) == 0) {
11         std::cout << "要修改的用户不存在" << std::endl;
12         mysql_free_result(result);
13         return;
14     }
15     mysql_free_result(result);
16     sprintf(query, "update user set UserPass='%s',ConnectionNum=%d,UseFor=%d where UserName='%s'", body.userPass, body.connection, body.type, body.userName);
17     if (mysql_query(&m_sqlCon, query)) {
18         std::cout << "修改失败" << std::endl;
19         return;
20     }
21     std::cout << "修改成功,共修改:" << mysql_affected_rows(&m_sqlCon) << "行" << std::endl;
22 }

.使用mysql_connect(卡塔尔(英语:State of Qatar)函数连接MySQL服务器:mysql_connect("hostname", "username","password");如,$link = mysql_connect("localhost", "root", ""卡塔尔(英语:State of Qatar) or die("不能够连接纳数据库服务器!大概是数据库服务器并未有运行,也许顾客名密码有误!".mysql_error;

 

.使用mysql_select_db(卡塔尔函数选拔数据库文件:mysql_query;

5.mysql的去除操作delete

如,$db_selected=mysql_query;

实际上删除、改过和插入操作都以相符的,会在那之中三个别样相应都差不离了,作者也就径直贴出代码了。

.使用mysql_query(卡塔尔(英语:State of Qatar)函数推行SQL语句:mysql_query,$link);

代码如下:

累加会员:$result=mysql_query("insert into tb_member values;

 1 void dataBase::user_delete(char *userName) {
 2     char query[255];
 3     char select_user[255];
 4     MYSQL_RES *result;
 5     sprintf(select_user, "select * from user where UserName='%s'", userName);
 6     if (mysql_query(&m_sqlCon, select_user)||!(result = mysql_store_result(&m_sqlCon))) {
 7         std::cout << "删除查询失败" << std::endl;
 8         return;
 9     }
10     if (mysql_num_rows(result)==0) {
11         std::cout << "要删除的用户不存在" << std::endl;
12         mysql_free_result(result);
13         return;
14     }
15     mysql_free_result(result);
16     sprintf(query, "delete from user where UserName='%s'", userName);
17     if (mysql_query(&m_sqlCon, query)) {
18         std::cout << "删除失败" << std::endl;
19         return;
20     }
21     std::cout << "删除成功,共删除:" << mysql_affected_rows(&m_sqlCon) << "行" << std::endl;
22 }

改正会员:$result=mysql_query("update tb_member setuser='b',pwd=''where user='a'",$link);

 

去除会员:$result=mysql_query("delecte from tb_member where user='b'",$link);

如上算是对Mysql中常用的风流浪漫对代码的下结论,有哪些不圆满的期待大家能够建议来,也指望这几个能在平时帮到大家,起到一个模板的效果,也能便于大家对mysql的读书。作者也归于一个learner,也可望能和贵族在平时中互相学习。

询问会员:$sql=mysql_query("select * from tb_book");

转发请注脚出处:。谢谢

似是而非查询:$sql=mysql_query("select * from tb_book where bookname like '%".trim;

 

//通用符%代表零个或私下多个字符。

来得表布局:$result=mysql_query;

.使用mysql_fetch_array(卡塔尔函数从数组结果集中拿到音信:

语法构造:array mysql_fetch_array(resource result[,int result_type])

参数result能源类型的参数,整形型参数,要传播的是由mysql_fetch_array(卡塔尔(英语:State of Qatar)函数再次回到的数目指针;

参数result_type:可选用,php操作MySQL数据库语句功底整数型参数,要传播的是MYSQL_ASSOC MYSQL_BOTH

<>$sql=mysql_query("select * from tb_book");$info=mysql_fetch_object;<>$sql=mysql_query("select * from tb_book where bookname like '%".trim;$info=mysql_fetch_object;

.使用mysql_fetch_object(卡塔尔国函数从结果聚焦获取风流洒脱行作为对象:

语法构造:object mysql_fetch_object;

<>$sql=mysql_query("select * from tb_book");$info=mysql_fetch_object;<>$sql=mysql_query("select * from tb_book where bookname like '%".trim;$info=mysql_fetch_object;

mysql_fetch_object()函数与mysql_fetch_array(卡塔尔函数相同,独有有些有别于,即再次回到多个对象实际不是数组,该函数只好通过字段名来访谈数组。访谈结果聚焦行的因素的语法构造:$row->col_name

.使用mysql_fetch_row(卡塔尔(قطر‎函数逐行拿到结果聚集的每条记下:

语法布局:array mysql_fetch_row

<>$sql=mysql_query("select * from tb_book");$row=mysql_fetch_row;<>$sql=mysql_query("select * from tb_book where bookname like '%".trim;$row=mysql_fetch_row;

.使用mysql_num_rows(卡塔尔(英语:State of Qatar)函数获取结果聚集地记录数:

语法构造:int mysql_num_rows

$sql=mysql_query("select * from tb_book");......

注:若要得到insert、update、delete语句的所影响到的数码,则必得使用mysql_affected_rows(卡塔尔国函数来落到实处。

.mysql_query;//设置MySQL的编码格式为 gb类型,以隐瞒乱码。

.关闭记录集:mysql_free_result;

.关闭MySQL数据库服务器:mysql_close;

本文由澳门新葡萄京娱乐网站发布于MySQL数据库,转载请注明出处:澳门新葡萄京888官网动用Mysql的详实步骤及种种常

关键词: