Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7934395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:38:07+00:00 2026-06-03T21:38:07+00:00

I have a sql file (alice.sql) which looks like this in the editor if

  • 0

I have a sql file (alice.sql) which looks like this in the editor

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK_dg_object_extern_pub_dg_extern_pub_status]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[dg_object_extern_pub] DROP CONSTRAINT FK_dg_object_extern_pub_dg_extern_pub_status GO

if I load that file into irb it looks like this

 f = File.open("alice.sql").readlines

it looks like this 🙁

=> ["\377\376i\000f\000 \000e\000x\000i\000s\000t\000s\000 \000(\000s\000e\000l\000e\000c\000t\000 \00

I wanted to search and replace some strings in the file but this seems impossible now

any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:38:09+00:00Added an answer on June 3, 2026 at 9:38 pm

    With f = File.open("alice.sql").readlines you open a file handle, but never close it.

    You should do:

    f = File.open("alice.sql")
    lines = f.readlines
    f.close
    

    or

    File.open("alice.sql"){|f|
      lines = f.readlines
    }
    

    With File#readlines you get an array of lines. If you want to do replacements in a string, you shoudl use readinstead:

    File.open("alice.sql"){|f|
      content = f.read
    }
    

    And last, but not least: Your alice.sql seems to be UTF16, so you must read it as UTF-16:

    File.open("alice.sql", :encoding => 'UTF-16BE:UTF-8'){|f|
      content = f.read
    }
    

    Now you get \uFEFFif exists (sele... You see the leading BOM?
    To get rid of it, use:

    File.open("alice.sql", :encoding => 'BOM|UTF-16BE:UTF-8'){|f|
      content = f.read
    }
    

    (Needs ruby 1.9, maybe the BOM-version requires 1.9.3).

    If you need the content outside the block, the variable must be defined outside the block (or you use File#close)

    content = nil #define variable, so you get the content after the open-block
    File.open("alice.sql", :encoding => 'BOM|UTF-16BE:UTF-8'){|f|
      content = f.read
    }
    p content
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL file which will give me an output like below: 10|1
I have this sql file: USE mydb; DROP PROCEDURE IF EXISTS execSql; DELIMITER //
i have an sql file of the following form: CREATE VIEW dbo.SomeView AS select
I have an .sql file which contains some data like first_name, last_name, email etc.
I have sql output delimited file like this: FD LD -------- -------- 02-01-11 02-28-11
I have a sql-file containing several statements like this: create table user ( name
I have a .sql file from a MySQL dump containing tables, definitions and data
I have a rather large SQL file which starts with the byte order marker
I have created a batch file which automatically copy a .sql file to the
I have an SQL file which uses declares a cursor and I am running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.