1. 處理數據批量生成sql插入語句
處理數據批量生成sql插入語句
最近在做一個天氣預報模塊,首先需要將客戶端公網ip轉換成所在城市,然後將所在城市名轉換成對應的城市代碼,在網上找到了城市代碼,但是需要處理一下,看了看,有三百多城市及對應的城市代碼,想存到資料庫。就想著做一個數據處理自動生成sql語句的工具,提高效率。
1 直轄市
2 "北京","上海","天津","重慶"
3 "101010100","101020100","101030100","101040100"
4
5 特別行政區
6 "香港","澳門"
7 "101320101","101330101"
8
9 黑龍江
10 "哈爾濱","齊齊哈爾","牡丹江","大慶","伊春","雙鴨山","鶴崗","雞西","佳木斯","七台河","黑河","綏化","大興安嶺"
11 "101050101","101050201","101050301","101050901","101050801","101051301","101051201","101051101","101050401","101051002","101050601","101050501","101050701"
12
13 吉林
14 "長春","延吉","吉林","白山","白城","四平","松原","遼源","大安","通化"
15 "101060101","101060301","101060201","101060901","101060601","101060401","101060801","101060701","101060603","101060501"
16
17 遼寧
18 "沈陽","大連","葫蘆島","盤錦","本溪","撫順","鐵嶺","遼陽","營口","阜新","朝陽","錦州","丹東","鞍山"
19 "101070101","101070201","101071401","101071301","101070501","101070401","101071101","101071001","101070801","101070901","101071201","101070701","101070601","101070301"
20
21 內蒙古
22 "呼和浩特","呼倫貝爾","錫林浩特","包頭","赤峰","海拉爾","烏海","鄂爾多斯","通遼"
23 "101080101","101081000","101080901","101080201","101080601","101081001","101080301","101080701","101080501"
24
25 河北
26 "石家莊","唐山","張家口","廊坊","邢台","邯鄲","滄州","衡水","承德","保定","秦皇島"
27 "101090101","101090501","101090301","101090601","101090901","101091001","101090701","101090801","101090402","101090201","101091101"
28
29 河南
30 "鄭州","開封","洛陽","平頂山","焦作","鶴壁","新鄉","安陽","濮陽","許昌","漯河","三門峽","南陽","商丘","信陽","周口","駐馬店"
31 "101180101","101180801","101180901","101180501","101181101","101181201","101180301","101180201","101181301","101180401","101181501","101181701","101180701","101181001","101180601","101181401","101181601"
32
33 山東
34 "濟南","青島","淄博","威海","曲阜","臨沂","煙台","棗庄","聊城","濟寧","菏澤","泰安","日照","東營","德州","濱州","萊蕪","濰坊"
35 "101120101","101120201","101120301","101121301","101120710","101120901","101120501","101121401","101121701","101120701","101121001","101120801","101121501","101121201","101120401","101121101","101121601","101120601"
36
37 山西
38 "太原","陽泉","晉城","晉中","臨汾","運城","長治","朔州","忻州","大同","呂梁"
39 "101100101","101100301","101100601","101100401","101100701","101100801","101100501","101100901","101101001","101100201","101101101"
40
41 江蘇
42 "南京","蘇州","崑山","南通","太倉","吳縣","徐州","宜興","鎮江","淮安","常熟","鹽城","泰州","無錫","連雲港","揚州","常州","宿遷"
43 "101190101","101190401","101190404","101190501","101190408","101190406","101190801","101190203","101190301","101190901","101190402","101190701","101191201","101190201","101191001","101190601","101191101","101191301"
44
45 安徽
46 "合肥","巢湖","蚌埠","安慶","六安","滁州","馬鞍山","阜陽","宣城","銅陵","淮北","蕪湖","毫州","宿州","淮南","池州"
47 "101220101","101221601","101220201","101220601","101221501","101221101","101220501","101220801","101221401","101221301","101221201","101220301","101220901","101220701","101220401","101221701"
48
49 陝西
50 "西安","韓城","安康","漢中","寶雞","咸陽","榆林","渭南","商洛","銅川","延安"
51 "101110101","101110510","101110701","101110801","101110901","101110200","101110401","101110501","101110601","101111001","101110300"
52
53 寧夏
54 "銀川","固原","中衛","石嘴山","吳忠"
55 "101170101","101170401","101170501","101170201","101170301"
56
57 甘肅
58 "蘭州","白銀","慶陽","酒泉","天水","武威","張掖","甘南","臨夏","平涼","定西","金昌"
59 "101160101","101161301","101160401","101160801","101160901","101160501","101160701","101050204","101161101","101160301","101160201","101160601"
60
61 青海
62 "西寧","海北","海西","黃南","果洛","玉樹","海東","海南"
63 "101150101","101150801","101150701","101150301","101150501","101150601","101150201","101150401"
64
65 湖北
66 "武漢","宜昌","黃岡","恩施","荊州","神農架","十堰","咸寧","襄陽","孝感","隨州","黃石","荊門","鄂州"
67 "101200101","101200901","101200501","101201001","101200801","101201201","101201101","101200701","101200201","101200401","101201301","101200601","101201401","101200301"
68
69 湖南
70 "長沙","邵陽","常德","郴州","吉首","株洲","婁底","湘潭","益陽","永州","岳陽","衡陽","懷化","韶山","張家界"
71 "101250101","101250901","101250601","101250501","101251501","101250301","101250801","101250201","101250701","101251401","101251001","101250401","101251201","101250202","101251101"
72
73 浙江
74 "杭州","湖州","金華","寧波","麗水","紹興","衢州","嘉興","台州","舟山","溫州"
75 "101210101","101210201","101210901","101210401","101210801","101210501","101211001","101210301","101210601","101211101","101210701"
76
77 江西
78 "南昌","萍鄉","九江","上饒","撫州","吉安","鷹潭","宜春","新余","景德鎮","贛州"
79 "101240101","101240901","101240201","101240301","101240401","101240601","101241101","101240501","101241001","101240801","101240701"
80
81 福建
82 "福州","廈門","龍岩","南平","寧德","莆田","泉州","三明","漳州"
83 "101230101","101230201","101230701","101230901","101230301","101230401","101230501","101230801","101230601"
84
85 貴州
86 "貴陽","安順","赤水","遵義","銅仁","六盤水","畢節","凱里","都勻"
87 "101260101","101260301","101260208","101260201","101260601","101260801","101260701","101260501","101260401"
88
89 四川
90 "成都","瀘州","內江","涼山","阿壩","巴中","廣元","樂山","綿陽","德陽","攀枝花","雅安","宜賓","自貢","甘孜州","達州","資陽","廣安","遂寧","眉山","南充"
91 "101270101","101271001","101271201","101271601","101271901","101270901","101272101","101271401","101270401","101272001","101270201","101271701","101271101","101270301","101271801","101270601","101271301","101270801","101270701","101271501","101270501"
92
93 廣東
94 "廣州","深圳","潮州","韶關","湛江","惠州","清遠","東莞","江門","茂名","肇慶","汕尾","河源","揭陽","梅州","中山","德慶","陽江","雲浮","珠海","汕頭","佛山"
95 "101280101","101280601","101281501","101280201","101281001","101280301","101281301","101281601","101281101","101282001","101280901","101282101","101281201","101281901","101280401","101281701","101280905","101281801","101281401","101280701","101280501","101280800"
96
97 廣西
98 "南寧","桂林","陽朔","柳州","梧州","玉林","桂平","賀州","欽州","貴港","防城港","百色","北海","河池","來賓","崇左"
99 "101300101","101300501","101300510","101300301","101300601","101300901","101300802","101300701","101301101","101300801","101301401","101301001","101301301","101301201","101300401","101300201"
100
101 雲南
102 "昆明","保山","楚雄","德宏","紅河","臨滄","怒江","曲靖","思茅","文山","玉溪","昭通","麗江","大理"
103 "101290101","101290501","101290801","101291501","101290301","101291101","101291201","101290401","101290901","101290601","101290701","101291001","101291401","101290201"
104
105 海南
106 "海口","三亞","儋州","瓊山","通什","文昌"
107 "101310101","101310201","101310205","101310102","101310222","101310212"
108
109 新疆
110 "烏魯木齊","阿勒泰","阿克蘇","昌吉","哈密","和田","喀什","克拉瑪依","石河子","塔城","庫爾勒","吐魯番","伊寧"
111 "101130101","101131401","101130801","101130401","101131201","101131301","101130901","101130201","101130301","101131101","101130601","101130501","101131001"
112
113 西藏
114 "拉薩","阿里","昌都","那曲","日喀則","山南","林芝"
115 "101140101","101140701","101140501","101140601","101140201","101140301","101140401"
116
117 台灣
118 "台北","高雄"
119 "101340102","101340201"
城市代碼
一看上去很亂的,而且對應關系是每個省城市一行,代碼一行,分別用引號引起,用逗號分隔,每行間都沒有符號分隔,省名沒有用引號。首先是想著把省名去掉,因為每個城市名都是不相同的。想著每兩行兩行的去處理,但是也要費不少功夫,還容易出錯。就想個索性一次性的全處理的演算法。
ps:界面很簡單,上面是輸入數據,中間是轉換,下面是輸出數據。
後台主要代碼:
[csharp] view plain
private void button1_Click(object sender, EventArgs e)
{
string data = textBox1.Text.Replace("r", "").Replace("n", "").Replace("t", "").Replace(" ", "").Replace(" ", "").Replace(" ", "");
MatchCollection matchsdata = matches(data, ""[sS]*?"");
string[,] temps = new string[matchsdata.Count / 2, 2];
int count0 = 0;
int count1 = 0;
string input = string.Empty;
foreach (Match m in matchsdata)
{
string tempdata = m.Value.Replace(""", "");
try
{
int tryp = int.Parse(tempdata);
temps[count1, 1] = tempdata;
count1++;
}
catch (Exception ex)
{
temps[count0, 0] = tempdata;
count0++;
}
}
for (int i = 0; i < (matchsdata.Count / 2); i++)
{
input += "insert into tbl_CityCode(c_city,c_code) values( + temps[i, 0] + , + temps[i, 1] + )rn";
}
textBox2.Text = input;
}
public static MatchCollection matches(string str, string exp)
{
return Regex.Matches(str, exp, RegexOptions.IgnoreCase);
}
首先是將輸入的數據處理,去除換行符,空格什麼的。然後你應該是會得到一行數據,然後通過正則表達式匹配出所有帶引號的數據,你會發現需要的數據全部都是用引號引起來的,但是怎樣區分城市名和城市代碼呢,它們是混在一起的。不用擔心,你發現了嗎?城市名是字元串,城市代碼是一串數字,我們只要將匹配出的數據數組遍歷,每一行數據都去轉換成int類型,這樣城市名的行就會報錯,在catch中捕捉,這一行就是城市名,沒錯的就是城市代碼,把數據一次存到一個二維數組,對應的列中就行了。這樣就會獲得了相對應的城市名和城市代碼。生成的sql語句要對應相應的資料庫表。
表結構:
轉換完了將生成的sql語句放到查詢器中執行就ok了。共處理了349個城市。
最後不放心自己的演算法,隨機抽查了幾條數據,沒有錯誤。
<script type="text/javascript"><!-- google_ad_client = "ca-pub-1944176156128447"; /* cnblogs 首頁橫幅 */ google_ad_slot = "5419468456"; google_ad_width = 728; google_ad_height = 90; //--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
2. java如何根據實體類圖生成sql腳本
自動生成C#和Java實體類,自動生連接符為「&」和「+」的INSERT,UPDATE,DELETE,SELECT語句,支持簡單的數據查詢修改。
3. oracle中如何通過已有的表生成相關sql語句
最簡單的方法,通過第三方工具plsql。
步驟:
1、登錄表所在資料庫及用戶。
4. 怎麼創建資料庫
問題一:創建資料庫的兩種方法 互動式創建,就是你右擊資料庫然後選擇新建資料庫按鈕就可以.....還有一種就是sql語句創建比如創建一個資料庫名為a 的資料庫,那麼sql語句就是,create database a;然後執行下就可以...
問題二:如何用MySQL建立資料庫 這很簡單啊!
在Windows下,假如你的MySQL裝在 D:\MySQL
就可以這樣:
先切換到它的bin目錄
cd D:\MySQL\bin\
撫:\MySQL\bin>mysql -u root -p
接著輸入你的root密碼
接下來你就可以創建資料庫、創建用戶、創建修改表之類的操作(常用命令如下)。
查看現有資料庫
mysql> show databases;
創建資料庫(假如資料庫名為 mydb)
mysql> create database mydb;
刪除資料庫(假如資料庫名為 mydb)
mysql> drop database accounts;
使用資料庫(假如使用資料庫 mydb)
mysql> use mydb;
執行完使用資料庫命令後,就可以對該資料庫進行創建、修改、插入、刪除表等操作,這些表的操作命令你可以到網上找找,不是很難的。一個資料庫就相當於一個 Excel 文件,而表則相當於Excel文件的單元格,數據就是存放在表中。
問題三:怎樣建立一個簡單資料庫? 把excel導入資料庫,不出現表格嵌套可以使用下面這個方法導入;
思路:
(1)、把excel數據讀入到dataset中;
(2)、建立相應結構的數據表格
(3)、把dat暢set中的數據更新到數據表中
問題四:如何用資料庫向導創建資料庫 Microsoft Access 提供了三種方法來創建 Access 資料庫 (Microsoft Access 資料庫:數據和對象(如表、查詢或窗體)組成的 *** ,與特定的主題或用途有關。)。
可以使用「資料庫向導」來創建資料庫。通過該向導可以從內置模板中進行選擇,然後對其進行一定程度的自定義。隨後,該向導會為資料庫創建一組表、查詢、窗體和報表,同時還會創建切換面板。表中不含任何數據。如果內置模板中的某個模板非常符合您的要求,請使用該方法。
使用「資料庫向導」創建資料庫
利用「資料庫向導」,用一步操作即可為所選資料庫類型創建必需的表、窗體和報表。這是創建資料庫的最簡單方法。該向導提供了有限的選項來自定義資料庫。
單擊工具欄上的「新建」。
在「新建文件」任務窗格中,在「模板」下,單擊「本機上的模板」。
在「資料庫」選項卡上,單擊要創建的資料庫類型的圖標,然後單擊「確定」。
在「文件新建資料庫」對話框中,指定資料庫的名稱和位置,然後單擊「創建」。
按照「資料庫向導」的指導進行操作。
如果向導未啟動
這可能是因為 Access 正運行於沙盒模式,但您的計算機上尚未安裝 Microsoft Jet 4.0 SP8 或更高版本。啟用沙盒模式後,必須安裝有 Jet 4.0 SP8 或更高版本,Access 才能完全發揮作用。
有關安裝 Jet 升級的詳細信息,請參閱 Office Online 文章關於 Microsoft Jet 4.0 SP8 或更高版本。
有關沙盒模式的詳細信息,請參閱 Office Online 文章關於 Microsoft Jet Expression Service 沙盒模式。
注釋 不能使用「資料庫向導」向已有的資料庫中添加新的表、窗體或報表。
如果正在使用 Access 2003,可以在 中搜索 Access 模板。下載模板是創建資料庫的最快方式。如果您找到了非常符合要求的模板,請使用該方法。模板是一個包含表、查詢、窗體和報表的 Access 資料庫文件 (*.mdb)。表中不含任何數據。打開資料庫後,可以自定義資料庫和對象。
使用模板創建資料庫
這是創建資料庫的最快方式。如果能找到並使用與您的要求非常接近的模板,則此方法效果最佳。
在工具欄上單擊「新建」。
在「新建文件」任務窗格中,在「模板」下,搜索特定的模板,或單擊「Office Online 模板」找到合適的模板。
找到需要的 Access 模板,然後單擊「下載」。
如果要基於自己的設計創建資料庫,請先創建一個空資料庫,然後再分別添加表、窗體、報表及其他對象。這是最靈活的方法,但需要分別定義每一個資料庫元素。
不使用「資料庫向導」創建空資料庫
單擊工具欄上的「新建」。
在「新建文件」任務窗格中的「新建」下,單擊「空資料庫」。
在「文件新建資料庫」對話框中,指定資料庫的名稱和位置,然後單擊「創建」。
然後將出現「資料庫」窗口 (資料庫窗口:在打開 Access 資料庫或 Access 項目時出現的窗口。它顯示用於新建資料庫對象和打開現有對象的快捷方式。),現在便可以在資料庫中創建所需的對象 (資料庫對象:Access 資料庫包含諸如表、查詢、窗體、報表、頁、宏和模塊等對象;Access 項目包含諸如窗體、報表、頁、宏和模塊等對象。)。...>>
問題五:怎樣在mysql中創建資料庫 是這句:創建一個資料庫MYSQLDATA
mysql> CREATE DATABASE MYSQLDATA;
基本操作:MySQL中新建用戶,新建資料庫,用戶授權,刪除用戶,修改密碼的相關操作測試環境:WIN32 mysql5.0.45註:本操作是在WIN命令提示符下,phpMyAdmin同樣適用。
用戶:phplamp 用戶資料庫:phplampDB1.新建用戶。登錄MYSQL
@>mysql -u root -p
@>密碼
創建用戶
mysql> insert into mysql.user(Host,User,Password) values(localhost,phplamp,password(1234));
刷新系統許可權表
mysql>flush privileges;
這樣就創建了一個名為:phplamp 密碼為:1234 的用戶。然後登錄一下。mysql>exit;
@>mysql -u phplamp -p
@>輸入密碼
mysql>登錄成功2.為用戶授權。登錄MYSQL(有ROOT許可權)。我里我以ROOT身份登錄.
@>mysql -u root -p
@>密碼
首先為用戶創建一個資料庫(phplampDB)
mysql>create database phplampDB;
授權phplamp用戶擁有phplamp資料庫的所有許可權。
>grant all privileges on phplampDB.* to identified by '1234';
刷新系統許可權表
mysql>flush privileges;
mysql>其它操作/*
如果想指定部分許可權給一用戶,可以這樣來寫:
mysql>grant select,update on phplampDB.* to identified by '1234';
刷新系統許可權表。
mysql>flush privileges;
*/3.刪除用戶。@>mysql -u root -p
@>密碼
mysql>DELETE FROM user WHERE User=phplamp and Host=localhost;
mysql>flush privileges;
刪除用戶的資料庫
mysql>drop database phplampDB;4.修改指定用戶密碼。@>mysql -u root -p
@>密碼
mysql>update mysql.user set password=password('新密碼') where User=phplamp and Host=localhost;
mysql>flush privileges;5.列出所有資料庫mysql>show database;6.切換資料庫mysql>use '資料庫名';7.列出所有表mysql>show t......>>
問題六:怎麼創建資料庫連接 方法一、通過編寫代碼來連接資料庫
1定義連接字元串。Data Source=伺服器;Initial Catalog=資料庫名;User ID=用戶名;Pwd=密碼 如:Data Source=IDEA-PC\SQLEXPRESS;Initial Catalog=student;User ID=sa;
2創建Connection對象。 SqlConnection sqlconnection1=new SqlConnection(constring);
constring為連接字元串。
3打開與資料庫的鏈接。 Sqlconnection1.Open();
4這時資料庫就連接成功,可以操作資料庫了。
方法二、通過拖拽形式建立資料庫連接
1打開視圖-->伺服器資源管理器。
2右鍵點就數據連接,選擇添加連接。
3選擇伺服器名(先刷新一下)-->登陸到伺服器身份驗證形式-->選擇一個資料庫名-->測試鏈接-->確定(如果測試鏈接成功的話)。
4這是建立資料庫連接就成功了,可以使用了。
問題七:sqlserver 下怎麼建立資料庫 怎麼建表 方法/步驟
1
首先我們打開SQL SERVER自帶的資料庫管理工具,從開始菜單中可以找到,如圖點擊進去;
2
開始連接SQL SERVER伺服器,就是我們裝好的SQL SERVER 伺服器;
3
右擊資料庫,選擇第一個,新建一個資料庫;
4
填寫資料庫的名稱,下面是設置自動增長的,一般不用管,默認
5
點擊確定後就可以生成一個資料庫,此時裡面是沒有表的;
6
右擊表新建一個表,填寫你要的欄位名稱
7
填完欄位名稱後點擊欄位名稱那個內部窗口的小叉叉,然後就提示你輸入表名了,填寫下表名,一個資料庫的建立過程就是這樣的;
或者
create database stuDB
on primary -- 默認就屬於primary文件組,可省略
(
/*--數據文件的具體描述--*/
name='stuDB_data', -- 主數據文件的邏輯名稱
filename='D:\stuDB_data.mdf', -- 主數據文件的物理名稱
size=5mb, --主數據文件的初始大小
maxsize=100mb, -- 主數據文件增長的最大值
filegrowth=15%--主數據文件的增長率
)
log on
(
/*--日誌文件的具體描述,各參數含義同上--*/
name='stuDB_log',
filename='D:\stuDB_log.ldf',
size=2mb,
filegrowth=1mb
)
問題八:oracle資料庫怎麼創建新用戶? 在CRT中進入oracle --連接sqlplus
按照下面寫完代碼直接復制進去回車就行了
---建立用戶代碼為:CREATE USER 用戶名字 PROFILE DEFAULT IDENTIFIED BY 密碼 DEFAULT TABLESPACE 指定表空間名字 TEMPORARY TABLESPACE 臨時表空間 ACCOUNT UNLOCK;
---賦權
begin
EXECUTE IMMEDIATE 'GRA�T SELECT ANY DICTIONARY TO 用戶名';
EXECUTE IMMEDIATE 'GRANT ALTER ANY TABLE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT ALTER SYSTEM TO 用戶名';
EXECUTE IMMEDIATE 'GRANT ALTER ANY PROCEDURE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CONNECT TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE ANY PROCEDURE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE ANY TABLE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE ANY INDEX TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE DATABASE LINK TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE PUBLIC DATABASE LINK TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE ANY VIEW TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE PUBLIC SYNONYM TO 用戶名';
EXECUTE IMMEDIATE 'GRANT CREATE TABLE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT DELETE ANY TABLE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT DROP PUBLIC DATABASE LINK TO 用戶名';
EXECUTE IMMEDIATE 'GRANT DROP ANY TRIGGER TO 用戶名';
EXECUTE IMMEDIATE 'GRANT DROP ANY INDEX TO 用戶名';
EXECUTE IMMEDIATE 'GRANT DROP ANY PROCEDURE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT DROP ANY TABLE TO 用戶名';
EXECUTE IMMEDIATE 'GRANT DROP ANY VIEW TO 用戶名';
......>>
問題九:如何創建資料庫 1、通過向導建立和使用資料庫
第一步:選擇服務視圖,然後展開DataBase(資料庫節點),右鍵點擊JavaDB
啟動伺服器
2、在右鍵單擊JavaDB,選擇創建資料庫
3、在彈出的對話框中填寫相應的名稱,寫完後點擊確定完成的數據的創建。