Database coding help

Hello there!

I wanted to add database operations to my code but i’m having issues with the data not being written to my db.

I have a string (outputrow) and a item separator (CSVsep), using those two i get an array of items which i need to write to my table.

Here is my code

[code] 'Engage db mode
If DBmode = True then

    Dim dbarray() as String
    Dim row As New DatabaseRecord
    Dim db As New MSSQLServerDatabase
    db.Host = "192.168.1.80\\SQLEXPRESS"
    db.Port = 1433
    db.DatabaseName = "filesrv"
    db.UserName = "xojo"
    db.Password = "xojo"
    
    If db.Connect Then
      'proceed with database operations
      'Convert row to array
      dbarray=outputrow.split(CSVsep)
      if Fullfile = true then
        row.column("Size") = dbarray(0)
        row.column("Date") = dbarray(1)
        row.column("Path") = dbarray(2)
        row.column("Filename") = dbarray(3)
        row.column("Pathlength") = dbarray(4)
      elseif Shortfile = true then
        row.column("Date") = dbarray(0)
        row.column("Path") = dbarray(1)
        row.column("Filename") = dbarray(2)
        row.column("Pathlength") = dbarray(3)
      end if
      db.InsertRecord("Xojo_data", row)
      If db.Error Then
        DBerror = 1
        DBerrormsg = db.ErrorMessage
      end if
    Else
      DBerror = 2
      DBerrormsg = db.ErrorMessage
    End If
    End If

[/code]

Any ideas on how to solve my issue?

I don’t get any error flag raised by my timer routines.

Thank you!

Use SQL instead
Make sure to COMMIT

Do your database columns allow null values? I see that in your ShortFile condition you don’t supply a value for the “Size” column. I’m not sure if that is your problem here but I’ve had that give me problems before.

I’ve just found why the data wasn’t written to my db, the date column require a date type not a string. What is the best way to convert a string to a dateformat?

ParseDate with exception handling.

http://documentation.xojo.com/index.php/ParseDate

[quote=180456:@Sascha S]ParseDate with exception handling.

http://documentation.xojo.com/index.php/ParseDate[/quote]

My date strings have this format:

2012/04/12 21:43:19

Making parsedate not compatible, crawling the forum i’ve found some code and created a working routine

[code] Dim filedate as New date
Dim datetime(-1) as string
Dim DateArray(-1) as string
Dim TimeArray(-1)as string
Datetime = dbarray(1).split(" “)
DateArray = Datetime(0).split(”/")
TimeArray = Datetime(1).split(":")

      Filedate.Day = Val(DateArray(2))
      Filedate.Month = Val(DateArray(1))
      Filedate.Year = Val(DateArray(0))
      Filedate.Hour = Val(TimeArray(0))
      Filedate.Minute = Val(TimeArray(1))
      Filedate.Second = Val(TimeArray(2))[/code]

Issue is I’m getting an error with xojo about having more than one item with the same name, commenting the row or setting to a fixed value solves the problem. Changing the sql column name or the date item name doesn’t.

Apparently xojo checks if the value is a string or not and rather than saying that i shouldn’t use that, it throws that duplicate string name exception. If i use date.sqldate or date.sqldatetime everything works. Thank you thread!