-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfrmConvertDB.vb
More file actions
890 lines (760 loc) · 36.4 KB
/
Copy pathfrmConvertDB.vb
File metadata and controls
890 lines (760 loc) · 36.4 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
Imports System.IO
Imports System.Data.OleDb
Imports System.Text
Imports Microsoft.Win32
Imports System.Data.SQLite
Public Class frmConvertDB
Dim conAccessDB As OleDbConnection
Dim conMain As New SQLiteConnection
Dim conGazetteer As New SQLiteConnection
Dim conVoice As New SQLiteConnection
Dim conTracks As New SQLiteConnection
Dim conMedia As New SQLiteConnection
Public Sub V2DB(ByVal bCopyAccessSpecified As Boolean)
Cursor = Cursors.WaitCursor
Dim bCopyAccess As Boolean = bCopyAccessSpecified
If bCopyAccess Then
If txtDB.Text.Length > 0 Then
'Test access DB connection
conAccessDB = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=" & txtDB.Text)
Try
conAccessDB.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to V1 Gilbert 21 DB: " & ex.Message)
Cursor = Cursors.Arrow
Exit Sub
End Try
Else
bCopyAccess = False
End If
End If
'Delete the SQLite DB if it already exists
If Not Directory.Exists(txtDBFolder.Text) Then
MessageBox.Show("The folder '" & txtDBFolder.Text & "' must be created first.")
Cursor = Cursors.Arrow
Exit Sub
End If
'The connection will create empty database files so just checking if the files are
'present isn't enough. In order to see if an active DB is present, we need to check
'if the DB files are over a certain size.
Dim fi As FileInfo
Dim bNonEmptyFile As Boolean = False
For Each strFile In Directory.GetFiles(txtDBFolder.Text, "*.db3")
fi = New FileInfo(strFile)
If fi.Length > 0 Then
bNonEmptyFile = True
End If
Next
If bNonEmptyFile Then
Dim response As MessageBoxButtons = MessageBox.Show("Database files already exist in '" & txtDBFolder.Text & "'. Are you sure that you want to overwrite them?", "Database exists", MessageBoxButtons.YesNo)
If response = Windows.Forms.DialogResult.No Then
Cursor = Cursors.Arrow
Exit Sub
ElseIf response = Windows.Forms.DialogResult.Yes Then
Dim strFile2 As String
For Each strFile2 In Directory.GetFiles(txtDBFolder.Text, "*.db3")
File.Delete(strFile2)
Next
Else
'Windows.Forms.DialogResult.No - just carry on
End If
End If
'Test SQLite DB connections (will create DBs if not already exist)
conMain.ConnectionString = "Data Source=" & Path.Combine(txtDBFolder.Text, "g21main.db3") & ";"
Try
conMain.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to Gilbert 21 main DB: " & ex.Message)
Cursor = Cursors.Arrow
Exit Sub
End Try
conGazetteer.ConnectionString = "Data Source=" & Path.Combine(txtDBFolder.Text, "g21gazetteer.db3") & ";"
Try
conGazetteer.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to Gilbert 21 gazetteer DB: " & ex.Message)
Cursor = Cursors.Arrow
Exit Sub
End Try
conVoice.ConnectionString = "Data Source=" & Path.Combine(txtDBFolder.Text, "g21voice.db3") & ";"
Try
conVoice.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to Gilbert 21 gazetteer DB: " & ex.Message)
Cursor = Cursors.Arrow
Exit Sub
End Try
conTracks.ConnectionString = "Data Source=" & Path.Combine(txtDBFolder.Text, "g21tracks.db3") & ";"
Try
conTracks.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to Gilbert 21 tracks DB: " & ex.Message)
Cursor = Cursors.Arrow
Exit Sub
End Try
conMedia.ConnectionString = "Data Source=" & Path.Combine(txtDBFolder.Text, "g21media.db3") & ";"
Try
conMedia.Open()
Catch ex As Exception
MessageBox.Show("Failed to connect to Gilbert 21 media DB: " & ex.Message)
Cursor = Cursors.Arrow
Exit Sub
End Try
'Create and copy tables
CreateAndCopyRecordsTable(bCopyAccess)
CreateAndCopyHashCodesTable(bCopyAccess)
CreateAndCopyos50kgazTable(bCopyAccess)
CreateAndCopyosDistrictVectorGazTable(bCopyAccess)
CreateAndCopyTaxonDictionaryTable(bCopyAccess)
CreateAndCopyWhereClauseTable(bCopyAccess)
CreateAndPopulateVoiceTable(bCopyAccess)
CreateTrackTables(bCopyAccess)
CreateMultiMediaTables(bCopyAccess)
lblProgress.Text = "Processing complete."
Cursor = Cursors.Arrow
MessageBox.Show("Database creation is complete.", "Gilbert 21")
Me.Close()
End Sub
Private Sub CreateTrackTables(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing Track table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conTracks.CreateCommand()
'Create the Tracks table
SQLcommand.CommandText = "CREATE TABLE Tracks (" & _
" FileID INTEGER PRIMARY KEY AUTOINCREMENT," & _
" Filename TEXT," & _
" OriginalPath TEXT," & _
" CurrentPath TEXT," & _
" DateCreated DATETIME," & _
" FileType TEXT," & _
" GenericFileType Text" & _
");"
SQLcommand.ExecuteNonQuery()
'Create the RecordTracks table
SQLcommand.CommandText = "create table RecordTracks (RecordID INTEGER, FileID INTEGER);"
SQLcommand.ExecuteNonQuery()
End Sub
Private Sub CreateMultiMediaTables(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing multi-media table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conMedia.CreateCommand()
'Create the Tracks table
SQLcommand.CommandText = "CREATE TABLE Media (" & _
" FileID INTEGER PRIMARY KEY AUTOINCREMENT," & _
" Filename TEXT," & _
" Comment TEXT," & _
" FullPath TEXT," & _
" RelativePath TEXT," & _
" Data BLOB," & _
" Thumbnail BLOB," & _
" DateCreated DATETIME," & _
" FileType TEXT," & _
" GenericFileType Text" & _
");"
SQLcommand.ExecuteNonQuery()
'Create the RecordMedia table
SQLcommand.CommandText = "create table RecordMedia (RecordID INTEGER, FileID INTEGER, [Order] INTEGER);"
SQLcommand.ExecuteNonQuery()
End Sub
Private Sub CreateAndCopyRecordsTable(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing Records table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conMain.CreateCommand()
'Create the Records table
Dim strSQL As String = "CREATE TABLE Records (" & _
" ID INTEGER PRIMARY KEY AUTOINCREMENT," & _
" IsTempRow BOOLEAN," & _
" Filename TEXT," & _
" FileIndex INTEGER," & _
" FileLon NUMERIC," & _
" FileLat NUMERIC," & _
" DateTimeKey TEXT," & _
" Entered DATE," & _
" Modified DATE," & _
" NoExport BOOLEAN," & _
" Recorder TEXT," & _
" Determiner TEXT," & _
" Confirmer TEXT," & _
" CommonName TEXT," & _
" ScientificName TEXT," & _
" TaxonGroup TEXT," & _
" Abundance TEXT," & _
" Units TEXT," & _
" Location TEXT," & _
" Town TEXT," & _
" GridRef TEXT," & _
" Lon NUMERIC," & _
" Lat NUMERIC," & _
" RecDate DATE," & _
" RecTime TEXT," & _
" TimeZone TEXT," & _
" Comment TEXT," & _
" PersonalNotes TEXT " & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
If bCopyAccess Then
'Copy the Access DB to SQLite
Dim daAccessDB As OleDbDataAdapter = New OleDbDataAdapter("Select * from Records", conAccessDB)
Dim dtInput As DataTable = New DataTable
Try
daAccessDB.Fill(dtInput)
Catch ex As Exception
MessageBox.Show("Record selection query failed: " & ex.Message)
Exit Sub
End Try
Dim dtRow As DataRow
Dim i As Integer = 0
Dim paramID As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramID)
Dim paramIsTempRow As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramIsTempRow)
Dim paramFilename As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramFilename)
Dim paramFileIndex As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramFileIndex)
Dim paramFileLon As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramFileLon)
Dim paramFileLat As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramFileLat)
Dim paramDateTimeKey As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramDateTimeKey)
Dim paramEntered As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramEntered)
Dim paramModified As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramModified)
Dim paramNoExport As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramNoExport)
Dim paramRecorder As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramRecorder)
Dim paramDeterminer As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramDeterminer)
Dim paramConfirmer As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramConfirmer)
Dim paramCommonName As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramCommonName)
Dim paramScientificName As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramScientificName)
Dim paramTaxonGroup As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramTaxonGroup)
Dim paramAbundance As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramAbundance)
Dim paramUnits As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramUnits)
Dim paramLocation As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramLocation)
Dim paramTown As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramTown)
Dim paramGridRef As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramGridRef)
Dim paramRecDate As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramRecDate)
Dim paramRecTime As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramRecTime)
Dim paramComment As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramComment)
Dim paramPersonalNotes As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramPersonalNotes)
pbConvert.Maximum = dtInput.Rows.Count
pbConvert.Value = 0
Dim SQLtransaction As SQLite.SQLiteTransaction
SQLtransaction = conMain.BeginTransaction()
paramIsTempRow.Value = False
For Each dtRow In dtInput.Rows
paramID.Value = ModVal(dtRow("ID"))
paramFilename.Value = ModVal(dtRow("Filename"))
paramFileIndex.Value = ModVal(dtRow("FileIndex"))
paramFileLon.Value = ModVal(dtRow("Lon"))
paramFileLat.Value = ModVal(dtRow("Lat"))
paramDateTimeKey.Value = ModVal(dtRow("DateTimeKey"))
paramEntered.Value = ModVal(dtRow("Entered"))
paramModified.Value = ModVal(dtRow("Modified"))
paramNoExport.Value = ModVal(False)
paramRecorder.Value = ModVal(dtRow("Recorder"))
paramDeterminer.Value = ModVal(dtRow("Determiner"))
paramConfirmer.Value = ModVal(dtRow("Confirmer"))
paramCommonName.Value = ModVal(dtRow("CommonName"))
paramScientificName.Value = ModVal(dtRow("ScientificName"))
paramTaxonGroup.Value = ModVal(dtRow("TaxonGroup"))
paramAbundance.Value = ModVal(dtRow("Abundance"))
paramUnits.Value = ModVal(dtRow("Units"))
paramLocation.Value = ModVal(dtRow("Location"))
paramTown.Value = ModVal(dtRow("Town"))
paramGridRef.Value = ModVal(dtRow("GridRef"))
paramRecDate.Value = ModVal(dtRow("RecDate"))
paramRecTime.Value = ModVal(dtRow("RecTime"))
paramComment.Value = ModVal(dtRow("Comment"))
paramPersonalNotes.Value = ModVal(dtRow("PersonalNotes"))
SQLcommand.CommandText = "INSERT INTO Records (ID,IsTempRow, Filename,FileIndex,FileLon,FileLat,DateTimeKey,Entered,Modified,NoExport, Recorder,Determiner,Confirmer,CommonName,ScientificName,TaxonGroup,Abundance,Units,Location,Town,GridRef,RecDate,RecTime,Comment,PersonalNotes) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
SQLcommand.ExecuteNonQuery()
pbConvert.Value += 1
Next
SQLtransaction.Commit()
End If
SQLcommand.Dispose()
End Sub
Private Function ModVal(ByVal objVal As Object) As Object
Dim objRet As Object = Nothing
If Not objVal Is Nothing Then
If Not objVal.ToString() = "Null" Then
objRet = objVal
End If
End If
Return objRet
End Function
Private Sub CreateAndCopyHashCodesTable(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing CodeHash table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conMain.CreateCommand()
'Create the Records table
Dim strSQL As String = "CREATE TABLE HashCodes (" & _
" ID INTEGER PRIMARY KEY," & _
" Hash TEXT," & _
" CommonName TEXT," & _
" ScientificName TEXT," & _
" TaxonGroup TEXT," & _
" Abundance TEXT," & _
" Units TEXT," & _
" Location TEXT," & _
" Town TEXT," & _
" RecDate DATE," & _
" GridRef TEXT," & _
" Comment TEXT" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
If bCopyAccess Then
'Copy the Access DB to SQLite
Dim daAccessDB As OleDbDataAdapter = New OleDbDataAdapter("Select * from HashCodes", conAccessDB)
Dim dtInput As DataTable = New DataTable
Try
daAccessDB.Fill(dtInput)
Catch ex As Exception
MessageBox.Show("Record selection query failed: " & ex.Message)
Exit Sub
End Try
Dim dtRow As DataRow
Dim i As Integer = 0
Dim paramID As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramID)
Dim paramHash As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramHash)
Dim paramCommonName As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramCommonName)
Dim paramScientificName As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramScientificName)
Dim paramTaxonGroup As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramTaxonGroup)
Dim paramAbundance As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramAbundance)
Dim paramUnits As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramUnits)
Dim paramLocation As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramLocation)
Dim paramTown As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramTown)
Dim paramRecDate As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramRecDate)
Dim paramGridRef As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramGridRef)
Dim paramComment As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramComment)
pbConvert.Maximum = dtInput.Rows.Count
pbConvert.Value = 0
Dim SQLtransaction As SQLite.SQLiteTransaction
SQLtransaction = conMain.BeginTransaction()
For Each dtRow In dtInput.Rows
paramID.Value = dtRow("ID")
paramHash.Value = dtRow("Hash")
paramCommonName.Value = dtRow("CommonName")
paramScientificName.Value = dtRow("ScientificName")
paramTaxonGroup.Value = dtRow("TaxonGroup")
paramAbundance.Value = dtRow("Abundance")
paramUnits.Value = dtRow("Units")
paramLocation.Value = dtRow("Location")
paramTown.Value = dtRow("Town")
paramRecDate.Value = dtRow("RecDate")
paramGridRef.Value = dtRow("GridRef")
paramComment.Value = dtRow("Comment")
SQLcommand.CommandText = "INSERT INTO HashCodes (ID,Hash,CommonName,ScientificName,TaxonGroup,Abundance,Units,Location,Town,RecDate,GridRef,Comment) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)"
SQLcommand.ExecuteNonQuery()
pbConvert.Value += 1
Next
SQLtransaction.Commit()
End If
SQLcommand.Dispose()
End Sub
Private Sub CreateAndCopyos50kgazTable(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing os50kgaz table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conGazetteer.CreateCommand()
'Create the Records table
Dim strSQL As String = "CREATE TABLE os50kgaz (" & _
" SEQ INTEGER," & _
" DEF_NAM TEXT," & _
" NORTH INTEGER," & _
" EAST INTEGER," & _
" COUNTY TEXT," & _
" FULL_COUNTY TEXT," & _
" F_CODE TEXT" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
If bCopyAccess Then
'Copy the Access DB to SQLite
Dim daAccessDB As OleDbDataAdapter = New OleDbDataAdapter("Select * from os50kgaz", conAccessDB)
Dim dtInput As DataTable = New DataTable
Try
daAccessDB.Fill(dtInput)
Catch ex As Exception
MessageBox.Show("Record selection query failed: " & ex.Message)
Exit Sub
End Try
Dim dtRow As DataRow
Dim i As Integer = 0
Dim paramSEQ As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramSEQ)
Dim paramDEF_NAM As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramDEF_NAM)
Dim paramNORTH As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramNORTH)
Dim paramEAST As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramEAST)
Dim paramCOUNTY As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramCOUNTY)
Dim paramFULL_COUNTY As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramFULL_COUNTY)
Dim paramF_CODE As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramF_CODE)
pbConvert.Maximum = dtInput.Rows.Count
pbConvert.Value = 0
Dim SQLtransaction As SQLite.SQLiteTransaction
SQLtransaction = conGazetteer.BeginTransaction()
For Each dtRow In dtInput.Rows
paramSEQ.Value = dtRow("SEQ")
paramDEF_NAM.Value = dtRow("DEF_NAM")
paramNORTH.Value = dtRow("NORTH")
paramEAST.Value = dtRow("EAST")
paramCOUNTY.Value = dtRow("COUNTY")
paramFULL_COUNTY.Value = dtRow("FULL_COUNTY")
paramF_CODE.Value = dtRow("F_CODE")
SQLcommand.CommandText = "INSERT INTO os50kgaz (SEQ,DEF_NAM,NORTH,EAST,COUNTY,FULL_COUNTY,F_CODE) VALUES(?,?,?,?,?,?,?)"
SQLcommand.CommandText = "INSERT INTO os50kgaz (SEQ,DEF_NAM,NORTH,EAST,COUNTY,FULL_COUNTY,F_CODE) VALUES(?,?,?,?,?,?,?)"
SQLcommand.ExecuteNonQuery()
pbConvert.Value += 1
Next
SQLtransaction.Commit()
End If
'Create the view os50kgazTownsCities
strSQL = "Create View os50kgazTownsCities as " & _
"SELECT os50kgaz.NORTH, os50kgaz.EAST, os50kgaz.DEF_NAM FROM os50kgaz " & _
"WHERE (((os50kgaz.F_CODE)='C' Or (os50kgaz.F_CODE)='T'));"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
End Sub
Private Sub CreateAndCopyosDistrictVectorGazTable(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing osDistrictVectorGaz table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conGazetteer.CreateCommand()
'Create the osDistrictVectorGaz table
Dim strSQL As String = "CREATE TABLE osDistrictVectorGaz (" & _
" ID INTEGER PRIMARY KEY," & _
" X INTEGER," & _
" Y INTEGER," & _
" NAME TEXT," & _
" Type TEXT" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
If bCopyAccess Then
'Copy the Access DB to SQLite
Dim daAccessDB As OleDbDataAdapter = New OleDbDataAdapter("Select * from osDistrictVectorGaz", conAccessDB)
Dim dtInput As DataTable = New DataTable
Try
daAccessDB.Fill(dtInput)
Catch ex As Exception
MessageBox.Show("Record selection query failed: " & ex.Message)
Exit Sub
End Try
Dim dtRow As DataRow
Dim i As Integer = 0
Dim paramID As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramID)
Dim paramX As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramX)
Dim paramY As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramY)
Dim paramNAME As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramNAME)
Dim paramType As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramType)
pbConvert.Maximum = dtInput.Rows.Count
pbConvert.Value = 0
Dim SQLtransaction As SQLite.SQLiteTransaction
SQLtransaction = conGazetteer.BeginTransaction()
For Each dtRow In dtInput.Rows
paramID.Value = dtRow("ID")
paramX.Value = dtRow("X")
paramY.Value = dtRow("Y")
paramNAME.Value = dtRow("NAME")
paramType.Value = dtRow("Type")
SQLcommand.CommandText = "INSERT INTO osDistrictVectorGaz (ID,X,Y,Name,Type) VALUES(?,?,?,?,?)"
SQLcommand.ExecuteNonQuery()
pbConvert.Value += 1
Next
SQLtransaction.Commit()
End If
SQLcommand.Dispose()
End Sub
Private Sub CreateAndCopyTaxonDictionaryTable(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing TaxonDictionary table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conMain.CreateCommand()
'Create the Records table
Dim strSQL As String = "CREATE TABLE TaxonDictionary (" & _
" ID INTEGER PRIMARY KEY," & _
" ScientificName TEXT," & _
" CommonName TEXT," & _
" TaxonGroup TEXT" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
If bCopyAccess Then
'Copy the Access DB to SQLite
Dim daAccessDB As OleDbDataAdapter = New OleDbDataAdapter("Select * from TaxonDictionary", conAccessDB)
Dim dtInput As DataTable = New DataTable
Try
daAccessDB.Fill(dtInput)
Catch ex As Exception
MessageBox.Show("Record selection query failed: " & ex.Message)
Exit Sub
End Try
Dim dtRow As DataRow
Dim i As Integer = 0
Dim paramID As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramID)
Dim paramCommonName As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramCommonName)
Dim paramScientificName As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramScientificName)
Dim paramTaxonGroup As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramTaxonGroup)
pbConvert.Maximum = dtInput.Rows.Count
pbConvert.Value = 0
Dim SQLtransaction As SQLite.SQLiteTransaction
SQLtransaction = conMain.BeginTransaction()
For Each dtRow In dtInput.Rows
paramID.Value = dtRow("ID")
paramCommonName.Value = dtRow("CommonName")
paramScientificName.Value = dtRow("ScientificName")
paramTaxonGroup.Value = dtRow("TaxonGroup")
SQLcommand.CommandText = "INSERT INTO TaxonDictionary (ID,CommonName,ScientificName,TaxonGroup) VALUES(?,?,?,?)"
SQLcommand.ExecuteNonQuery()
pbConvert.Value += 1
Next
SQLtransaction.Commit()
End If
SQLcommand.Dispose()
End Sub
Private Sub CreateAndCopyWhereClauseTable(ByVal bCopyAccess As Boolean)
lblProgress.Text = "Processing WhereClause table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conMain.CreateCommand()
'Create the Records table
Dim strSQL As String = "CREATE TABLE WhereClause (" & _
" QuerySQL TEXT, " & _
" QueryName TEXT" & _
");"
SQLcommand.CommandText = strSQL
SQLcommand.ExecuteNonQuery()
If bCopyAccess Then
'Copy the Access DB to SQLite
Dim daAccessDB As OleDbDataAdapter = New OleDbDataAdapter("Select * from WhereClause", conAccessDB)
Dim dtInput As DataTable = New DataTable
Try
daAccessDB.Fill(dtInput)
Catch ex As Exception
MessageBox.Show("Record selection query failed: " & ex.Message)
Exit Sub
End Try
Dim dtRow As DataRow
Dim i As Integer = 0
Dim paramQuerySQL As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramQuerySQL)
Dim paramQueryName As New SQLite.SQLiteParameter()
SQLcommand.Parameters.Add(paramQueryName)
pbConvert.Maximum = dtInput.Rows.Count
pbConvert.Value = 0
Dim SQLtransaction As SQLite.SQLiteTransaction
SQLtransaction = conMain.BeginTransaction()
For Each dtRow In dtInput.Rows
paramQuerySQL.Value = dtRow("QuerySQL")
paramQueryName.Value = dtRow("QueryName")
SQLcommand.CommandText = "INSERT INTO WhereClause (QuerySQL,QueryName) VALUES(?,?)"
SQLcommand.ExecuteNonQuery()
pbConvert.Value += 1
Next
SQLtransaction.Commit()
End If
SQLcommand.Dispose()
End Sub
Private Function PrepareValForSQLite(ByVal objVal As Object) As Object
If cfun.HasNoValue(objVal) Then
Return objVal
ElseIf objVal.GetType Is GetType(String) Then
Return objVal.Replace("'", "''")
Else
Return objVal
End If
End Function
Private Sub frmConvertDB_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
InitialiseFormFields()
End Sub
Public Sub InitialiseFormFields()
txtDBFolder.Text = frmOptions.txtDBFolder.Text
'Read V1 DB from registry settings
Dim key As RegistryKey = Registry.CurrentUser.OpenSubKey("Software", True)
Dim newkey As RegistryKey = key.CreateSubKey("Gilbert21")
txtDB.Text = newkey.GetValue("Database")
End Sub
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
V2DB(True)
End Sub
Private Sub butBrowseDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBrowseDB.Click
OpenFileDialog.Filter = "Access DB (*.mdb)|*.mdb|All files (*.*)|*.*"
OpenFileDialog.FilterIndex = 1
If Not OpenFileDialog.ShowDialog() = DialogResult.Cancel Then
txtDB.Text = OpenFileDialog.FileName
End If
End Sub
Private Sub CreateAndPopulateVoiceTable(ByVal bCopyAccess As Boolean)
Cursor = Cursors.WaitCursor
lblProgress.Text = "Creating and populating voice table..."
Me.Refresh()
'Create the SQLiteCommand object
Dim SQLcommand As SQLiteCommand
SQLcommand = conVoice.CreateCommand()
'Create the Records table
SQLcommand.CommandText = "create table Sounds (SoundID INTEGER PRIMARY KEY AUTOINCREMENT," & _
"OriginalFilename TEXT, OriginalTrackFilename TEXT," & _
"[Date] DATE, [Time] TEXT, FileLon FLOAT, FileLat FLOAT, Sound BLOB);"
SQLcommand.ExecuteNonQuery()
'Create a new table to cross-reference WAV files and records
SQLcommand.CommandText = "create table RecordSounds (RecordID INTEGER, SoundID INTEGER,[Order] INTEGER);"
'"foreign key(SoundID) references Sounds(SoundID));"
SQLcommand.ExecuteNonQuery()
If bCopyAccess Then
'Populate new tables
'Go through each file in Gilbert21.voice and make an entry in the sounds table
Dim strDBFolder As String = Path.GetDirectoryName(txtDB.Text)
Dim strDBName As String = Path.GetFileName(txtDB.Text)
Dim strVoiceFolderName As String = strDBName.Substring(0, strDBName.Length - 3) & "voice"
Dim strVoiceFolder = Path.Combine(strDBFolder, strVoiceFolderName)
If Directory.Exists(strVoiceFolder) Then
Dim folder1 As String
Dim folder2 As String
Dim soundFile As String
Dim soundFileName As String
Dim intSoundID As Integer
Dim intRecordID As Integer
Dim intOrder As Integer
Dim strSplit() As String
Dim SQLcommandSounds As SQLiteCommand
SQLcommandSounds = conVoice.CreateCommand()
Dim SQLcommandRecordSounds As SQLiteCommand
SQLcommandRecordSounds = conVoice.CreateCommand()
Dim paramNull As New SQLite.SQLiteParameter()
SQLcommandSounds.Parameters.Add(paramNull)
paramNull = Nothing
Dim paramOriginalFilename As New SQLite.SQLiteParameter()
SQLcommandSounds.Parameters.Add(paramOriginalFilename)
Dim paramSound As New SQLite.SQLiteParameter()
SQLcommandSounds.Parameters.Add(paramSound)
paramSound.DbType = DbType.Binary
Dim paramRecordID As New SQLite.SQLiteParameter()
SQLcommandRecordSounds.Parameters.Add(paramRecordID)
Dim paramSoundID As New SQLite.SQLiteParameter()
SQLcommandRecordSounds.Parameters.Add(paramSoundID)
Dim paramOrder As New SQLite.SQLiteParameter()
SQLcommandRecordSounds.Parameters.Add(paramOrder)
Dim intFiles As Integer = 0
For Each folder1 In Directory.GetDirectories(strVoiceFolder)
If Not folder1.EndsWith("TempVoice") Then
For Each folder2 In Directory.GetDirectories(folder1)
For Each soundFile In Directory.GetFiles(folder2)
intFiles += 1
Next
Next
End If
Next
pbConvert.Maximum = intFiles
pbConvert.Value = 0
Dim SQLtransaction As SQLite.SQLiteTransaction
SQLtransaction = conVoice.BeginTransaction()
For Each folder1 In Directory.GetDirectories(strVoiceFolder)
If Not folder1.EndsWith("TempVoice") Then
For Each folder2 In Directory.GetDirectories(folder1)
For Each soundFile In Directory.GetFiles(folder2)
soundFileName = soundFile.Substring(folder2.Length + 1)
paramOriginalFilename.Value = soundFileName
FileToBlobParam(soundFile, paramSound)
SQLcommandSounds.CommandText = "INSERT INTO Sounds (SoundID, OriginalFilename, Sound) VALUES(?,?,?)"
SQLcommandSounds.ExecuteNonQuery()
'SQLcommandSounds.CommandText = "select max(SoundID) from Sounds"
SQLcommandSounds.CommandText = "Select last_insert_rowid()"
intSoundID = SQLcommandSounds.ExecuteScalar()
'Get recordID and order from sound filename
strSplit = soundFileName.Substring(0, soundFileName.Length - 4).Split("-")
intRecordID = strSplit(0)
intOrder = strSplit(1)
paramRecordID.Value = intRecordID
paramSoundID.Value = intSoundID
paramOrder.Value = intOrder
SQLcommandRecordSounds.CommandText = "insert into RecordSounds (RecordID, SoundID, [Order]) Values(?,?,?)"
SQLcommandRecordSounds.ExecuteNonQuery()
pbConvert.Value += 1
Next
Next
End If
Next
SQLtransaction.Commit()
SQLcommand.Dispose()
SQLcommandSounds.Dispose()
SQLcommandRecordSounds.Dispose()
End If
End If
Cursor = Cursors.Arrow
End Sub
Public Sub FileToBlobParam(ByVal filePath As String, ByRef param As SQLiteParameter)
Dim fs As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim bm() As Byte = br.ReadBytes(fs.Length)
br.Close()
fs.Close()
param.Value = bm
End Sub
Public Sub ExecuteSQLiteNonQuery(ByVal cmd As SQLiteCommand)
Try
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show("SQLite command '" & cmd.CommandText & "' failed: " & ex.Message)
Exit Sub
End Try
End Sub
End Class