06. november 2002 - 11:29Der er
14 kommentarer og 2 løsninger
Brug for hjælp med at indsamle date fra en loop
Jeg har en database med en masse medlemmer i, i et stykke kode laver jeg en loop hvor jeg udtrækker medlemsnavnene samt hvor jeg laver en input sætning ud for hver enkelt.
Hvordan samler jeg dataerne ud fra inputsætningerne og flytter dem ind i en database?
Her er koden jeg har brugt hidtil:
kode 1:
<% set conn = server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)}; dbq=" & server.mappath("data.mdb") sqlstmt = "select * from members order by membername" conn.execute (sqlstmt) set rs = conn.execute(sqlstmt) %>
jag førsøger igen, men min ASP er ikke så go som den skall vare så der kommer lite små fejl som du får fixa.
kode 2:
set sql = "" set conn = server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)}; dbq=" & server.mappath("data.mdb") sqlstmt = select membername from members order by membername set rs = conn.execute (sqlstmt)
do while not rs.eof
sql = sql & "update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "';"
rs.movenext loop rs.close
conn.execute(sql)
hvis du har en database som ikke støder multiple statements er du nødt till att byde sql = sql & "update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "';"
mod ett conn.execute(update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "'")
og ta bort conn.execute(sql)
tror det skall virke hvis du retter nogle små fejl
set conn = server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)}; dbq=" & server.mappath("data.mdb") sqlstmt = "select membername from members order by membername" set rs = conn.execute (sqlstmt) do while not rs.eof sqlstmt = sqlstmt & "update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "';" rs.movenext loop rs.close conn.execute (sqlstmt) conn.close set conn = nothing set sqlstmt = nothing response.redirect ("raidpointscorrect.asp") %>
Men jeg får følgende fejl:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'membernameupdate members set memberraidpoint = 0 where membername like 'Appoccolipstick Thunderbunz''.
en streng anvænds før att hente data i databasen och sen bygger du op en anden sql streng med de data du hentet, og sen ecekverar du den nya strengen...
sql = "" set conn = server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)}; dbq=" & server.mappath("data.mdb") sqlstmt = "select membername from members order by membername" set rs = conn.execute (sqlstmt) do while not rs.eof sql = sql & "update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "';" rs.movenext loop rs.close conn.execute (sql) conn.close set conn = nothing set sqlstmt = nothing response.redirect ("raidpointscorrect.asp") %>
Ny fejlmelding:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Characters found after end of SQL statement.
ok, det betyder nok att du ikke kan lave multiple statements
du får ersætta: sql = sql & "update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "';"
med: conn.execute("update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "'")
Dessvære kan du ikke med Access lave X antal SQL sætninger i en lan streng som man kan ved MSSSQL/MySQL. Så du skal for hver opdater databasen:
<% response.buffer = true
sql = "" set conn = server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)}; dbq=" & server.mappath("data.mdb") sqlstmt = "select membername from members order by membername" set rs = conn.execute (sqlstmt) do while not rs.eof sql = "update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "';" conn.execute (sql) rs.movenext loop rs.close conn.close set conn = nothing set sqlstmt = nothing response.redirect ("raidpointscorrect.asp") %>
Jeg har opdateret min kode som du foreslår, og får så denne fejl:
Microsoft OLE DB Provider for ODBC Drivers error '80040e10'
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
/prot/raidpointssavecorrected.asp, line 11
Min kode ser nu således ud:
<% response.buffer = true
sql = "" set conn = server.createobject("adodb.connection") conn.open "driver={microsoft access driver (*.mdb)}; dbq=" & server.mappath("data.mdb") sqlstmt = "select membername from members order by membername" set rs = conn.execute (sqlstmt) do while not rs.eof sql = "update members set memberraidpoint = " & request.form(rs("membername")) & " where membername like '" & rs("membername") & "';" conn.execute (sql) rs.movenext loop rs.close conn.close set conn = nothing set sqlstmt = nothing response.redirect ("raidpointscorrect.asp") %>
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.