Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB 6
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Pro VB 6 For advanced Visual Basic coders working in version 6 (not .NET). Beginning-level questions will be redirected to other forums, including Beginning VB 6.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 17th, 2006, 06:15 AM
Friend of Wrox
 
Join Date: Mar 2005
Location: , , .
Posts: 264
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem writing listview data to mysql db

Hi all i have difficulty writing each row of listview to mysql db. I be happy if some one help me fix it.Thanks



Code:
Private Sub Command11_Click()
 Dim CNN As ADODB.Connection
Set CNN = New ADODB.Connection
CNN.Open "DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=visualbasic6;USER=root;PASSWORD=;OPTION=3;"


 Dim sSQL As String
Dim i As Long
For i = 1 To ListView1.ListItems.Count
    sSQL = "INSERT INTO musictest (url,songname,artist,album) "
    sSQL = sSQL & " VALUES('" & ListView1.ListItems.Item(i).Text & "', '"
    sSQL = sSQL & " " & ListView1.Subitems(1) &¬†¬†', '"
    sSQL = sSQL & " & ListView1.Subitems(2) &¬†¬†', '"
    sSQL = sSQL & " & ListView1.Subitems(3) &¬†¬†', '"

    MsgBox sSQL
    'etc
    CNN.Execute sSQL
Next
    ¬†¬†
MsgBox " The songs are inserted to database successfully"
End Sub
error pic:
http://i5.photobucket.com/albums/y18...orlistview.jpg

and points to :CNN.Execute sSQL

i printed out the sql and the url field was used corectly but not the rest:
pic of sql statement:
[IMG] http://i5.photobucket.com/albums/y18...7/sqlprint.jpg [/IMG]


I even tried this and got error:


Code:
sSQL = sSQL & " VALUES('" & ListView1.ListItems.Item(i).Text & "','" & ListView1.SubItems(1) & "','" & ListView1.SubItems(2) & "', '" & ListView1.SubItems(3) & "')"
Code:
Compile error:

Method or data member not found
pointing at :.SubItems

could u help me fix this.thanks
Reply With Quote
  #2 (permalink)  
Old May 17th, 2006, 09:51 AM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In your first sample it appears you have several problems:

1 - The syntax is incorrect for working with a listview
2 - Your use of quotes in building the string is incorrect

The code should look more like this:


Code:
Private Sub Command1_Click()

    Dim sSQL As String    †
    Dim i As Long
    Dim item As ListItem    ' Here is the listitem you need to work with

    For i = 1 To ListView1.ListItems.Count
        Set item = ListView1.ListItems(i)
        sSQL = "INSERT INTO musictest (url,songname,artist,album) "
        sSQL = sSQL & " VALUES('" & item.Text & "', '"
        sSQL = sSQL & item.SubItems(1) & "', '"
        sSQL = sSQL & item.SubItems(2) & "', '"
        sSQL = sSQL & item.SubItems(3) & "')"

        Debug.Print sSQL
    Next

End Sub
Of course, you will have to put your data access code back in - I removed it for clarity.

I hope that helps

Woody Z http://www.learntoprogramnow.com
Reply With Quote
  #3 (permalink)  
Old April 10th, 2007, 07:17 PM
Registered User
 
Join Date: Apr 2007
Location: , , Canada.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Concatenating a sql statement as shown is a very bad idea.
What if one of the strings contain a quote?
It screws up. Use parameterized queries!!!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve image data from mysql db vermaarun2005 PHP Databases 0 July 14th, 2007 11:12 AM
Drawing Data from mysql DB to use on app majika MySQL 2 November 1st, 2006 08:27 AM
problem writing listview data to mysql db method Pro VB Databases 0 May 17th, 2006 06:18 AM
Writing to an msAccess db from txtBox Data sconineuk ADO.NET 0 March 8th, 2006 02:13 PM
problem with uploading blob data to MySQL DB kyle_shea PHP Databases 1 March 8th, 2005 05:15 PM



All times are GMT -4. The time now is 11:04 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.