Re: How to store text files in the postgresql?

From: Johan Nel <johan555(dot)nel555(at)xsinet555(dot)co(dot)za>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to store text files in the postgresql?
Date: 2009-06-07 11:18:25
Message-ID: h0g7pb$di0$1@news.eternal-september.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter Eisentraut 2009-06-07 14:10:59 Re: xml to table (as oppose to table to xml)
Previous Message Florian Weimer 2009-06-07 09:10:57 Re: How to store text files in the postgresql?