Search Postgresql Archives

Re: How to store text files in the postgresql?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.

--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux