mysql – 青春部落,流年似水 http://www.youthtribe.com 青春是一场远行,总记不起来时的路。 Thu, 07 Jul 2016 08:33:00 +0000 zh-CN hourly 1 https://wordpress.org/?v=6.1.7 mysql – 青春部落,流年似水 http://www.youthtribe.com/archives/1532 http://www.youthtribe.com/archives/1532#respond Thu, 07 Jul 2016 08:33:00 +0000 http://www.youthtribe.com/?p=1532 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>无标题文档</title>
</head>

<body>
<?php
//mysql

$dsn = ‘mysql:host=localhost’.’;dbname=name’;

$pdo = new PDO($dsn, “username”, “ps”);
$pdo->query(“set names utf8”);
/////////////////////////////////////
$handle = fopen(“ips.txt”, “r”);
$i = 0;
if ($handle) {
while (!feof($handle))
{
/*
if ($i >100)
{
break;
}
$i++;
*/
$str = fgets($handle, 4096);
echo $str.”<br>”;
$ip_start = substr($str,0,15);
$ip_end = substr($str,16,15);
$ip_start = str_replace(” “,””,$ip_start);
$ip_end = str_replace(” “,””,$ip_end);

$county_region = substr($str,32,strlen($str)-1);
//$county_region = mb_convert_encoding($county_region, “utf-8”, “utf-8”);
echo $ip_start.”==”.$ip_end.”==”.$county_region.”<br>”;

$ip_start_to_long_int = ip_to_long_int($ip_start);
$ip_end_to_long_int = ip_to_long_int($ip_end);

//insert into mysql
$sql = “insert into `ips`(`ip_start`,`ip_start_number`,`ip_end`,`ip_end_number`,`info`) values( ‘$ip_start’,$ip_start_to_long_int ,’$ip_end’,$ip_end_to_long_int,’$county_region’)”;
$res = $pdo->exec($sql);
if ($res == 0)
{
echo $sql.”<br>”;
}
}
}
echo “ok”;
function ip_to_long_int($ip){

$arr = explode(‘.’,$ip);
$res = $arr[0]*255*255*255 + $arr[1]*255*255 + $arr[2]*255 + $arr[3];
return $res;

}
?>
</body>
</html>

]]>
http://www.youthtribe.com/archives/1532/feed 0
mysql – 青春部落,流年似水 http://www.youthtribe.com/archives/1377 http://www.youthtribe.com/archives/1377#respond Wed, 05 Nov 2014 08:36:44 +0000 http://www.youthtribe.com/?p=1377 vc如何链接mysql数据库,这篇文章就是一个简单的示例。

mysql的头文件,lib,dll文件,我是用的wamp集成包的,msyql版本是 mysql5.6.17 。之前也试过其他集成包,发现mysql的一些文件是没有的(不是精简掉了),所以建议下wamp吧。wamp sourceforge 官网:http://sourceforge.net/projects/wampserver/files/WampServer%202/WampServer%202.1/

  • 找到mysql文件夹下的【include】文件夹,放到我们的vc工程目录下
  • 设置vc项目属性,添加包含。如下图
  •  在用到msyql的文件中,加下以下代码
  • #include “mysql.h”

  • 声明一个mysql变量
  • MYSQL mysql;

  • 新建一个数据库,当然还有对应的用户名。我的一律是 “s”。
  • 我在对话框初始化函数中加入以下代码
  • 	//开始链接数据库
    	mysql_init(&mysql);
    	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf-8"); 
    
    	if(!mysql_real_connect(&mysql,"localhost","user","password","user_db",3306,NULL,0))
    	{
    		AfxMessageBox("数据库连接失败");
    	}
    	else
    	{
    		//MessageBox("链接数据库成功!");
    	}
    

    草,不写了,难产了。。。因为现在我用的这个wamp也是不完整的,我用了之前 的才配配置好。狗日的。。。

    ]]>
    http://www.youthtribe.com/archives/1377/feed 0
    mysql – 青春部落,流年似水 http://www.youthtribe.com/archives/1282 http://www.youthtribe.com/archives/1282#respond Thu, 07 Aug 2014 12:13:19 +0000 http://www.youthtribe.com/?p=1282 vc操作mysql的典型例子
    1.包含文件

    #include "mysql.h"
    

    2.初始化数据库

    	//开始链接数据库
    	//MYSQL mysql;//定义成全局的
    	mysql_init(&mysql);
    	mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf-8"); 
    
    	if(!mysql_real_connect(&mysql,"localhost","user","password","user_db",3306,NULL,0))
    	{
    		AfxMessageBox("数据库连接失败");
    	}
    	else
    	{
    		//MessageBox("链接数据库成功!");
    	}
    

    3.插入数据

    	//添加到数据库
    	
    	CString strSQL = "insert into xbzq.group values(NULL,'test');";
    	const char * q =strSQL;
    	mysql_query(&mysql,q);
    

    4.读出数据

    	strsql = "select * from sentence_lib where id = " + m_nId;
    	mysql_query(&mysql,strsql);
    	res = mysql_store_result(&mysql);
    	MYSQL_ROW row=NULL;
    	row = mysql_fetch_row(res);
    	strpassage = strpassage+row[1];
    
    
    
    

    /////////////////////

    	//从数据库中随机取出一个句子
    	CString strsql="select * from sentence_lib";
    	const char * q =strsql;
    	mysql_query(&mysql,q);
    
    	MYSQL_RES *res = mysql_store_result(&mysql);
    
    	//mysql_use_result(MYSQL *mysql);
    
    	int nRow = res->row_count;//得到组文章用的句子库的数量
    	
    
    ]]>
    http://www.youthtribe.com/archives/1282/feed 0
    mysql – 青春部落,流年似水 http://www.youthtribe.com/archives/1227 http://www.youthtribe.com/archives/1227#respond Mon, 26 May 2014 15:28:17 +0000 http://www.youthtribe.com/?p=1227 方法二:
    通过登录mysql系统,
    mysql -uroot -p
    Enter password: 【输入原来的密码】
    mysql>use mysql;
    mysql> update user set password=passworD(“test”) where user=’root’;
    mysql> flush privileges;
    mysql> exit;

    ]]>
    http://www.youthtribe.com/archives/1227/feed 0