i am connecting to a mysql table through vba in excel and i am updating it:
Set cn = New ADODB.Connection
cn.Open "DRIVER={MySQL ODBC 5.1 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=employees;" & _
"USER=root;" & _
"PASSWORD=M1llen;" & _
"Option=3"
'lets get the batch info
'
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "batchinfo", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table from Report 1
'Set wsSheet1 = wbBook.Worksheets(1)
' better refer by name
'Set wsSheet1 = wbBook.Worksheets.("Report 1")
Worksheets.Item("Report 1").Select
dpath = Range("B2").Text
atime = Trim(Range("B3").Text)
rtime = Trim(Range("B4").Text)
lcalib = Trim(Range("B5").Text)
aname = Trim(Range("B6").Text)
rname = Trim(Range("B7").Text)
bstate = Trim(Range("B8").Text)
instrument = GetInstrFromXML()
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("datapath") = "abc"
.Fields("analysistime") = atime
.Fields("reporttime") = rtime
.Fields("lastcalib") = lcalib
.Fields("analystname") = aname
.Fields("reportname") = rname
.Fields("batchstate") = bstate
.Fields("instrument") = instrument
.Update ' stores the new record
End With
the issue is that the only field that gets updated is the instrument field!!
here i a desc of the batchinfo table mysql:
mysql> desc batchinfo;
+--------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+----------------+
| rowid | int(11) | NO | PRI | NULL | auto_increment |
| datapath | text | YES | | NULL | |
| analysistime | text | YES | | NULL | |
| reporttime | text | YES | | NULL | |
| lastcalib | text | YES | | NULL | |
| analystname | text | YES | | NULL | |
| reportname | text | YES | | NULL | |
| batchstate | text | YES | | NULL | |
| instrument | text | YES | | NULL | |
+--------------+---------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
funny thing is that when i recreate the table without the auto_increment then it works fine
i really need someone to answer this question, i dont care if you have a hunch or not exactly sure, i will try any solution
I'm not familiar with MySQL but TEXT looks like a blob type? If so I'm surprised it works at all as ADO requires special handling for BLOBS ( http://dev.mysql.com/tech-resources/articles/vb-blob-handling.html )
Try a VARCHAR type instead.
You could also try ADOCn.Execute "INSERT ..."
Related
I have a Access DB with following scheme:
+---------+---------+-----------+------------+-------------------------------------------+
| BrandNr | TextNr | LangCode | OngoingNr | Text |
+---------+---------+-----------+------------+-------------------------------------------+
| 1 | AB | 1 | 1 | Text beginns here but it doesn't end here |
| 1 | AB | 1 | 2 | Text isn't finished so need second row |
| 1 | AC | 2 | 1 | New text |
| 2 | hg2 | 1 | 1 | New brand new text |
+---------+---------+-----------+------------+-------------------------------------------+
Now I need to merge the text where BrandNR, TextNr and LangCode are the same. The Text should be ordered by the OngoingNr.
Some ideas?
I believe this will require VBA.
One possible method is to call a VBA function which defines a Static string-type variable that is initialised to an empty string for every new combination of BrandNR, TextNr and LangCode, else concatenated with itself.
Open the VBA IDE using Alt+F11
Insert a new Public Module Alt+I,M
Copy the following basic code into the new Module:
Function Concat(strInp As String, lngOrd As Long) As String
Static strTmp As String
If lngOrd = 1 Then strTmp = strInp Else strTmp = strTmp & " " & strInp
Concat = strTmp
End Function
In MS Access, create a new query with the following SQL, changing MyTable to the name of your table:
select q.BrandNr, q.TextNr, q.LangCode, Concat(q.Text,q.OngoingNr) as Merged
from
(
select t.* from MyTable t
order by t.BrandNr, t.TextNr, t.LangCode, t.OngoingNr
) q
I've been looking around the Internet for quite some time now, but I can't find a way to get a list of just column names. I don't care about the data each column has.
I want to take that list and compare it against a collection. I'm using the VB.NET MySqlConnector. I'm new to using the connector.
Edit:
Dim mscCMD As MySqlCommand = New MySqlCommand("SHOW COLUMNS FROM OCN.cpu", msc)
Dim sqlReader As MySqlDataReader = mscCMD.ExecuteReader()
Dim b As Integer = 0
Dim c As Integer = 0
While sqlReader.Read
msProjects(c) = sqlReader.**Item**(b)
b += 1
c += 1
End While
Never mind, I figured it out. I had to choose Item, not getName.
Edit: Perhaps I didn't just yet. It's reading one row. I'm unsure how to move to the next row. For example
mysql> SHOW COLUMNS FROM City;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
**Id | int(11) | NO | PRI | NULL | auto_increment**
| Name | char(35) | NO | | | |
| Country | char(3) | NO | UNI | | |
| District | char(20) | YES | MUL | | |
| Population | int(11) | NO | | 0 | |
+------------+----------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
That's the row it reads it reads. Once you go past the 5th item, you're out of bounds.
You can use the metadata from a result set:
final ResultSet rs = statement.executeQuery("SELECT * FROM `" + tableName +"` LIMIT 0,1;");
final ResultSetMetaData metaData = rs.getMetaData();
for(int i=1; i<metaData.getColumnCount(); i++)
{
System.out.println("" + metaData.getColumnName(i));
}
Also, you can get the java.sql.Types column type the same way.
Got it. Found the answer from a MS KB article.
https://support.microsoft.com/en-us/kb/310108
For Each myField In schemaTable.Rows
'For each property of the field...
For Each myProperty In schemaTable.Columns
'Display the field name and value.
Console.WriteLine(myProperty.ColumnName & " = " & myField(myProperty).ToString())
Next
Console.WriteLine()
'Pause.
Console.ReadLine()
Next
Specifically myField(myProperty).ToStringwill get you the column names. It will also contain other things about the column. You will have to sort through it, but all the column names from your table will be in there.
I am a fresh hand at django. I want to insert a new row into MySQL database, but when I trying to do as following, it goes error.
from django.db import models
...
class Msg(models.Model):
MsgId = BigIntegerField(length = 20)
ToUserName = CharField(max_length = 45)
FromUserName = CharField(max_length = 45)
Content = TextField(max_length = 1024, blank = True)
...
db_entry = Msg(MsgId=received_MsgId, ToUserName=received_ToUserName,
FromUserName=received_FromUserName, MsgType=received_MsgType,
Content=received_Content)
db_entry.save()
This following is the table all_massages existing in my database, and how can I add a new row to it, and what extra things I need to do.
+--------------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+-------+
| MsgId | bigint(20) unsigned | NO | PRI | NULL | |
| ToUserName | varchar(45) | NO | | NULL | |
| FromUserName | varchar(45) | NO | | NULL | |
| Content | text | YES | | NULL | |
+--------------+---------------------+------+-----+---------+-------+
If you have altered mode and executed syncdb ...it wont work....so delete the existing table and execute syncdb!!
Django documentation clearly specifies how to perform raw sql queries.
https://docs.djangoproject.com/en/dev/topics/db/sql/#performing-raw-sql-queries
For directly executing UPDATE, INSERT, or DELETE queries.
https://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly
Once again I would like to ask if anyone could help me on how would I insert and update it into my field column, this is bothering me for quite a while now.
the table looks like this;
----------------------------------------
id | type | multiplier | rdMultiplier |
----------------------------------------
1 | Reg | null | null |
2 | Spec | null | null |
3 | DReg | null | null |
4 | DSpec| null | null |
-----------------------------------------
I want to insert and update the 3rd and the 4th field.
Here is my code for getting the values coming from different textboxes:
$regularmul = $this->input->get('regularmulti');
$specialmul = $this->input->get('specialmulti');
$doubleregmul = $this->input->get('doubleregmulti');
$doublespecmul = $this->input->get('doublespecialmul');
$rd1 = $this->input->get('restmul1');
$rd2 = $this->input->get('restmul2');
$rd3 = $this->input->get('restmul3');
$rd4 = $this->input->get('restmul4');
Thank you for your patience in helping me
insert data
INSERT INTO TABLENAME(fieldname1,fieldname2,fieldname3,fieldname4) VALUES ('$variablename1','$variablename2','$variablename3','$variablename4');
update data
UPDATE TABLENAME SET fieldname1='$variablename1',fieldname2='$variablename2',fieldname3='$variablename3',fieldname4='$variablename4' WHERE field_id = 'so_and_so_id';
I have a few reports that I get every day that have to be imported into a database, MS Access, in a particular way. The problem is, the way the reports are laid out makes it difficult to do so.
Program: Name A
Date | Description | Other | Stuff
Date | Description | Other | Stuff
Program: Name B
Date | Description | Other | Stuff
Date | Description | Other | Stuff
Program: Name C
Date | Description | Other | Stuff
Date | Description | Other | Stuff
Basically, what I would like it to do is look like this.
Program: Name A | Date | Description | Other | Stuff
Program: Name A | Date | Description | Other | Stuff
Program: Name B | Date | Description | Other | Stuff
Program: Name B | Date | Description | Other | Stuff
Program: Name C | Date | Description | Other | Stuff
Program: Name C | Date | Description | Other | Stuff
I've played around with a couple different solutions, but so far nothing has done the job.
Thanks for any help!
Something like:
Dim xl As New Excel.Application
Dim ws As Excel.Worksheet
Dim rng As Excel.Range
Dim rs As DAO.Recordset
Dim db As Database
Set db = CurrentDb
''Run once
''s = "create table reporttable (id counter primary key, program text, " _
'' & "pdate text, [description] text, other text, stuff text)"
''db.Execute s, dbFailOnError
Set rs = db.OpenRecordset("ReportTable")
xl.Visible = True ''dev
Set ws = xl.Workbooks.Open("z:\docs\report.xlsx").Sheets("Sheet1")
Set rng = ws.UsedRange
For i = 1 To rng.Rows.Count
If rng.Cells(i, 1) Like "Program*" Then
progdat = rng.Cells(i, 1)
Else
rs.AddNew
rs!program = progdat
rs!pdate = rng.Cells(i, 1) ''Text, because you cannot trust reports
rs!Description = rng.Cells(i, 2)
rs!Other = rng.Cells(i, 3)
rs!Stuff = rng.Cells(i, 4)
rs.Update
End If
Next
Set rs = Nothing
Set rng = Nothing
ws.Parent.Close
Set ws = Nothing
xl.Quit
Set xl = Nothing