Jump to content
toarelli

Alterar Banco de Dados MySQL

Recommended Posts

toarelli

Caros amigos, por favor me perdoa por recorrer novamente a vossa ajuda.

Mas acho que sou muito burro, por não conseguir realizar tal alteração.

Estou com o seguinte problema.

1ª alteração na Base de Dados ficou assim:

..ID.... COD/DESCR......VALOR........FORNECEDOR

id=1 - 101 - Produto X - valor: 1,05 - Fornecedor: XYZ <== 1ª escolha de opção de compra

id=4 - 105 - Produto Z - valor: 1,00 - Fornecedor: ABC <== 1ª escolha de opção de compra

até aqui tudo bem

 

Infelizmente o item 101-Produto X, foi passado para outro fornecedor, ficando assim:

..ID.... COD/DESCR......VALOR........FORNECEDOR

id=2 - 101 - Produto X - valor: 1,05 - Fornecedor: ABC <== 2ª escolha de opção de compra

id=4 - 105 - Produto Z - valor: 1,00 - Fornecedor: ABC <== 1ª escolha de opção de compra - MANTIDA

 

Como faço para alterar no MySql passando:

o id=1 para cancelado

o id=2 para compra

 

Desculpa minha dúvida, mas infelizmente nessa parte estou apanhando para fazer.

Grato,

Toarelli

Share this post


Link to post
Share on other sites
Muryllo

Antes de mais nada, não faço ideia de como você lê/insere os dados no MySql. Deixo já aqui a forma como utilizo meu próprio wrapper para ler/escrever com maior facilidade usando o MySqlConnector para a framework 4.5. A linguagem que usei foi VB.NET. Quanto ao seu problema, me parece que precisará fazer uso do comando UPDATE. Com o comando update você atualiza o valor de uma coluna numa tabela.

Imports MySql.Data
Imports MySql.Data.MySqlClient
''' <summary>
''' Provides MySql wrapper for basic operations in database.
''' </summary>
''' <remarks></remarks>
Public Class MySqlAPI
    '
    Implements IDisposable
    '
    Private MySqlConn As MySqlConnection = New MySqlConnection
    Private MySqlAdapter As MySqlDataAdapter = New MySqlDataAdapter
    Private MySqlDataTable As DataTable = New DataTable
    Private MySqlComm As MySqlCommand = New MySqlCommand
    Private MySqlCommString As String = String.Empty
    '
    Private disposedValue As Boolean
    '
    ''' <summary>
    ''' Initialize the wrapper for new connection to specified database.
    ''' </summary>
    ''' <param name="sqlConnectionString"></param>
    ''' <param name="sqlCommandString"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal sqlConnectionString As String, ByVal sqlCommandString As String)
        MySqlConn = New MySqlConnection(sqlConnectionString)
        MySqlCommString = sqlCommandString
        MySqlConn.Open()
        MySqlComm = New MySqlCommand(sqlCommandString, MySqlConn)
    End Sub
    ''' <summary>
    ''' Initialize the wrapper for new connection to specified database.
    ''' </summary>
    ''' <param name="sqlConnectionString"></param>
    ''' <param name="sqlCommand"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal sqlConnectionString As String, ByVal sqlCommand As MySqlCommand)
        MySqlConn = New MySqlConnection(sqlConnectionString)
        MySqlConn.Open()
        MySqlComm = sqlCommand
        MySqlCommString = MySqlComm.CommandText
        MySqlComm.Connection = MySqlConn
    End Sub
    ''' <summary>
    ''' Initialize the wrapper for new connection to specified database.
    ''' </summary>
    ''' <param name="sqlConnection"></param>
    ''' <param name="sqlCommand"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal sqlConnection As MySqlConnection, ByVal sqlCommand As MySqlCommand)
        MySqlConn = sqlConnection
        If (MySqlConn.State <> ConnectionState.Open) Then
            If (MySqlConn.State = ConnectionState.Broken) Then
                MySqlConn.Close()
                MySqlConn.Open()
            End If
            If (MySqlConn.State = ConnectionState.Closed) Then
                MySqlConn.Open()
            End If
        End If
        MySqlComm = sqlCommand
        MySqlCommString = MySqlComm.CommandText
        MySqlComm.Connection = MySqlConn
    End Sub
    ''' <summary>
    ''' Initialize the wrapper for new connection to specified database.
    ''' </summary>
    ''' <param name="sqlConnection"></param>
    ''' <param name="sqlCommandString"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal sqlConnection As MySqlConnection, ByVal sqlCommandString As String)
        MySqlConn = sqlConnection
        MySqlCommString = sqlCommandString
        If (MySqlConn.State <> ConnectionState.Open) Then
            If (MySqlConn.State = ConnectionState.Broken) Then
                MySqlConn.Close()
                MySqlConn.Open()
            End If
            If (MySqlConn.State = ConnectionState.Closed) Then
                MySqlConn.Open()
            End If
        End If
        MySqlComm = New MySqlCommand(sqlCommandString, MySqlConn)
    End Sub
    '
    ''' <summary>
    ''' Returns the data table from MySql database.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MySql_GetDataTable() As DataTable
        Get
            MySqlAdapter = New MySqlDataAdapter(MySqlComm)
            MySqlDataTable = New DataTable
            MySqlAdapter.Fill(MySqlDataTable)
            Return MySqlDataTable
        End Get
    End Property
    '
    ''' <summary>
    ''' Returns the data table from MySql database.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MySql_GetDataTableAsync() As Task(Of Integer)
        Get
            MySqlAdapter = New MySqlDataAdapter(MySqlComm)
            MySqlDataTable = New DataTable
            Return MySqlAdapter.FillAsync(MySqlDataTable)
        End Get
    End Property
    '
    ''' <summary>
    ''' Returns the loaded data table.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MySql_ReadDataTable() As DataTable
        Get
            Return MySqlDataTable
        End Get
    End Property
    '
    ''' <summary>
    ''' Execute the specified command in MySql database.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MySql_ExecuteCommandAsync() As Task(Of Integer)
        Get
            Return MySqlComm.ExecuteNonQueryAsync
        End Get
    End Property
    '
    ''' <summary>
    ''' Execute the specified command in MySql database.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MySql_ExecuteCommand() As Integer
        Get
            Return MySqlComm.ExecuteNonQuery
        End Get
    End Property
    '
    ''' <summary>
    ''' Return the number of rows in datatable loaded.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MySql_GetRowsCount() As Integer
        Get
            If (MySqlDataTable IsNot Nothing) Then
                Return MySqlDataTable.Rows.Count
            Else
                Return 0
            End If
        End Get
    End Property
    '
    ''' <summary>
    ''' Change the MySqlConnection string.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public WriteOnly Property MySql_ChangeSqlConnectionString()
        Set(value)
            If (CStr(value) <> String.Empty) Then
                If (MySqlConn.State = ConnectionState.Open) Then
                    MySqlConn.Close()
                    MySqlConn.ConnectionString = CStr(value)
                    MySqlConn.Open()
                Else
                    MySqlConn.ConnectionString = CStr(value)
                    MySqlConn.Open()
                End If
            End If
        End Set
    End Property
    '
    ''' <summary>
    ''' Change the MySqlCommand string.
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public WriteOnly Property MySql_ChangeSqlCommandString
        Set(value)
            If (CStr(value) <> String.Empty) Then
                MySqlCommString = CStr(value)
                If (MySqlConn.State = ConnectionState.Open) Then
                    MySqlComm = New MySqlCommand(MySqlCommString, MySqlConn)
                Else
                    MySqlConn.Open()
                    MySqlComm = New MySqlCommand(MySqlCommString, MySqlConn)
                End If
            End If
        End Set
    End Property
    '
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                MySqlConn.Dispose()
                MySqlAdapter.Dispose()
                MySqlDataTable.Dispose()
                MySqlConn.Close()
            End If
        End If
        Me.disposedValue = True
    End Sub
    '
    ''' <summary>
    ''' Close the connection and dispose the wrapper.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
    '
End Class
  • Vote 1

Share this post


Link to post
Share on other sites
M6

Fazes um update.

Por exemplo, dizes que o valor da coluna Fornecedor passa a ser ABC para o produto com o id 4.

Algo do género:

update TABELA
set fornecedor = 'ABC'
where id = 3;

 


10 REM Generation 48K!
20 INPUT "URL:", A$
30 IF A$(1 TO 4) = "HTTP" THEN PRINT "400 Bad Request": GOTO 50
40 PRINT "404 Not Found"
50 PRINT "./M6 @ Portugal a Programar."

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.