Boardy
Boardy

Reputation: 36205

Saving file to MySQL Database

I am working on a c# project with a MySQL database. I have a log file and I want to store this file in the database.

I want to save the actual file into the database but what I've seen is you have to encode it to a string or write the bytes to a string and put that in the database, is that how it works to store the file, I thought you could just give the command the file path and it stores it in the database without me requiring to do all the encoding in the software.

If this makes any difference, it will need to be retrieved again from PHP, but I'm guessing that this shouldn't matter.

Upvotes: 1

Views: 2235

Answers (5)

Robert H
Robert H

Reputation: 11730

Why not use Log4Net? It has the ability to log to databases automatically. Take a look at https://logging.apache.org/log4net/release/config-examples.html for details on how to connect a database to Log4Net

orignal decayed link: http://logging.apache.org/log4net/release/sdk/log4net.Appender.AdoNetAppender.html

Upvotes: 0

RolandoMySQLDBA
RolandoMySQLDBA

Reputation: 44343

This may sound a little ludicrous but you should look into the LOAD_FILE function

Here is the example for the MySQL Documentation

mysql> UPDATE t
        SET blob_col=LOAD_FILE('/tmp/picture')
        WHERE id=1;

As long as the log file is small enough to fit into a BLOB, then that's your only shot at it

If the log file is too big, just archive the log file somewhere and save the file location instead

Upvotes: 1

qelper
qelper

Reputation: 91

Yes you have to encode it to string or bytes to store the file. Another way would be store the log file location, and instead save the log file on the disk some where.

Upvotes: 1

Icarus
Icarus

Reputation: 63956

If you want to store the contents of the file in the database, then yes, you either encode it or store the bytes in the table column. There's no way around that.

If you want to simply store the path to the file, then all you need to do is pass in the path as parameter to your MySQLCommand but the moment you move the file around in the filesystem, your record will be invalid, obviously.

Link to a somewhat related question regarding which approach is best: Storing the content of the file in the database or simply the path in the file system.

Upvotes: 0

Richard Hum
Richard Hum

Reputation: 651

Your program is going to have to parse the log file and turn it into an SQL query.

Upvotes: 0

Related Questions