Re: How to store text files in the postgresql? - Mailing list pgsql-general

From Johan Nel
Subject Re: How to store text files in the postgresql?
Date
Msg-id h0g7pb$di0$1@news.eternal-september.org
Whole thread Raw
In response to How to store text files in the postgresql?  (DimitryASuplatov <genesup@gmail.com>)
List pgsql-general
> 1/ Is it possible?
> 2/ Could you give me some quick tips on how to manage it from the start
> so that I knew what to look for in the manual?

Not sure how much you know about programming, but easiest will probably
be to have a small application.  Here is some code in the Npgsql library
documentation that shows how to do it in C#:

using System;
using System.Data;
using Npgsql;
using System.IO;
public class t
{
   public static void Main(String[] args)
   {
     NpgsqlConnection conn = new NpgsqlConnection(
       "server=localhost;user id=npgsql_tests;password=npgsql_tests");
     conn.Open();
     FileStream fs = new FileStream(args[0], FileMode.Open,
       FileAccess.Read);
     BinaryReader br = new BinaryReader(new BufferedStream(fs));
     Byte[] bytes = br.ReadBytes((Int32)fs.Length);
     Console.WriteLine(fs.Length);
     br.Close();
     fs.Close();
     NpgsqlCommand command = new NpgsqlCommand(
       "insert into tableBytea(field_bytea) values(:bytesData)", conn);
     NpgsqlParameter param = new NpgsqlParameter(
       ":bytesData", DbType.Binary);
     param.Value = bytes;
     command.Parameters.Add(param);
     command.ExecuteNonQuery();
     command = new NpgsqlCommand(
       "select field_bytea from tableBytea " +
       "where field_serial = (select max(select field_serial) from " +
       "tableBytea);", conn);
     Byte[] result = (Byte[])command.ExecuteScalar();
     fs = new FileStream(args[0] + "database", FileMode.Create,
       FileAccess.Write);
     BinaryWriter bw = new BinaryWriter(new BufferedStream(fs));
     bw.Write(result);
     bw.Flush();
     fs.Close();
     bw.Close();
     conn.Close();
   }
}

HTH,

Johan Nel
Pretoria, South Africa.

pgsql-general by date:

Previous
From: Grzegorz Jaśkiewicz
Date:
Subject: Re: xml to table (as oppose to table to xml)
Next
From: Dimitri Fontaine
Date:
Subject: Re: How to store text files in the postgresql?