Skip navigation

Tag Archives: mysql

Let’s say you have a store table in your database and you want to get the first letter of each store’s name. So you can create a menu by first letter of each store name and not having a letter that contains no stores.

1
2
3
4
5
$stores = $this->Store->find('all',array(    'order'=>'name ASC',
'fields'=>'DISTINCT(UPPER(LEFT(name,1))) AS letter',
'conditions'=>array("UPPER(LEFT(name,1)) REGEXP '[A-Z]'")));

Sometimes a client requires you to import some data from an excel sheet to mysql. You can do it by hand if there are only a few records. However, if there are a few hundred records, you need some kind of script to do this. I had this same problem. So i wrote something in autoit and thought i might share it. The script itself it very simple and very easy to modify.
You need to download and install autoit before you can run this. I could just give you the executable, but no one would download it and no one should. Besides the script is very specific and is meant to be modified to suit your need.

here the file in case wordpress butchers my script.

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <EditConstants.au3>

#include <Excel.au3>

#include <Array.au3>

#Include <Date.au3>


;file picker
$excelFile = FileOpenDialog("Select an excel file",@DesktopDir,"Excel (*.xls)" ,1+2)

If Not @error Then
;create main window
$mainGui = GUICreate("Importing",350,150)
GUISetState(@SW_SHOW)

$status = GUICtrlCreateEdit("Begin importing",10,10,330,130,$ES_AUTOVSCROLL+$ES_AUTOHSCROLL+$ES_MULTILINE+$ES_READONLY)

addStatus("File selected:"&amp;$excelFile)

doExport($excelFile)
Else
MsgBox(0,"Error","Error opening file")
Exit
EndIf

While 1
$msg = GUIGetMsg()
If $msg = $GUI_EVENT_CLOSE Then ExitLoop
WEnd
GUIDelete()

Func doExport($file)
$fpExcel = _ExcelBookOpen($file,0,1)
If @error = 1 Then
addStatus("Unable to Create the Excel Object")
Exit
ElseIf @error = 2 Then
addStatus("File does not exist - Shame on you!")
Exit
Else
addStatus("File open successfully")
EndIf

$done = False
$i = 2
While Not $done

;read a row from excel file
$excelArray = _ExcelReadArray($fpExcel,$i,1,9,0,0)

If $excelArray[0] &lt;&gt; "" Then
addStatus("reading Row " &amp; $i)
;insert into sql file
arrayToSql($excelArray)
Else
addStatus("Finsihed reading excel file")
addStatus($i-2 &amp; " rows converted into sql statement")
$done = True
EndIf
$i += 1
WEnd

_ExcelBookClose($fpExcel)
EndFunc

Func arrayToSql($array)
;save sql file to desktop
$sqlFile = FileOpen(@DesktopDir&amp;"/ExcelToSql-"&amp;@MDAY&amp;"-"&amp;@MON&amp;"-"&amp;@YEAR&amp;".sql",1+8)

$sqlStatement = "INSERT INTO `_products` (`int_id`, `product_type`, `title`, `description`, `synopsis`, `publication_date`, `price`, `categories`, `list`, `type_id`, `processed`, `write_err`) VALUES ("
$sqlStatement &amp;= "'"&amp; escapeString($array[0]) &amp; "'," ;int_id
$sqlStatement &amp;= "'"&amp; escapeString($array[1]) &amp; "'," ;product_type
$sqlStatement &amp;= "'"&amp; escapeString($array[2]) &amp; "'," ;title
$sqlStatement &amp;= "'"&amp; escapeString($array[3]) &amp; "'," ;description
$sqlStatement &amp;= "'"&amp; escapeString($array[4]) &amp; "'," ;synopsis
$sqlStatement &amp;= "'"&amp; toExcelDate($array[5]) &amp; "',"  ;date
$sqlStatement &amp;= "'"&amp; escapeString($array[6]) &amp; "'," ;price
$sqlStatement &amp;= "'"&amp; escapeString($array[7]) &amp; "'," ;category
$sqlStatement &amp;= "'"&amp; escapeString($array[8]) &amp; "'," ;list
$sqlStatement &amp;= "'','N','N'"
$sqlStatement &amp;= ");"

FileWriteLine($sqlFile,$sqlStatement)
FileClose($sqlFile)
EndFunc

Func escapeString($string)
;replace anything that is not a number, letter or common punctuation
$string = StringRegExpReplace($string,"[^0-9a-zA-Z.,\\/\!@#$%\^\&amp;\*\(\)\-_\+=\h&lt;&gt;]","")
;escape single quote
$string = StringRegExpReplace($string,"[']","\'")
Return $string

EndFunc

Func toExcelDate($dateString)
$yearString = StringMid($dateString,1,4)
$monthString = StringMid($dateString,5,2)
$dayString = StringMid($dateString,7,2)
return $dayString&amp;"-"&amp;StringMid(_DateToMonth($monthString,1),1,3)&amp;"-"&amp;$yearString

EndFunc

Func addStatus($text)
GUICtrlSetData($status,@CRLF&amp;$text,1)
EndFunc