1

I was trying to connect my .sql database with python version 2.7.9 with this code :

import sqlite3
db = sqlite3.connect("c:\\Users\\afsoon\\Desktop\\ch9learning\\sql\\genres.sql")
cursor = db.cursor()
query = """SELECT * FROM mytable where genre='folk'"""
lines = cursor.execute(query)
data = cursor.fetchall()
db.close()

but I catch this error :

lines = cursor.execute(query)
Traceback (most recent call last):

  File "<ipython-input-10-af02982f9f74>", line 1, in <module>
    lines = cursor.execute(query)

DatabaseError: file is encrypted or is not a database
 

I'm on windows and I've got sqlite3!

p.s: these are my database first lines

-- phpMyAdmin SQL Dump
-- version 4.5.1
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Dec 23, 2015 at 09:29 AM
-- Server version: 10.1.9-MariaDB
-- PHP Version: 5.6.15

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `genres`
--
CREATE DATABASE IF NOT EXISTS `genres` DEFAULT CHARACTER SET utf8 COLLATE utf8_persian_ci;
USE `genres`;

-- --------------------------------------------------------

--
-- Table structure for table `mytable`
--

CREATE TABLE `mytable` (
  `genre` varchar(640) COLLATE utf8_persian_ci DEFAULT NULL,
  `track_id` varchar(50) COLLATE utf8_persian_ci DEFAULT NULL,
  `artist_name` varchar(248) COLLATE utf8_persian_ci DEFAULT NULL,
  `title` varchar(248) COLLATE utf8_persian_ci DEFAULT NULL,
  `keyy` decimal(13,10) DEFAULT NULL,
  `modee` decimal(13,10) DEFAULT NULL,
  `duration` decimal(14,10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_persian_ci;

--
-- Dumping data for table `mytable`
Afsanefda
  • 3,069
  • 6
  • 36
  • 76
  • Are you sure the file exists? Is the path correct? Do you need the double slashes in the path name (not a Python expert)? – xaviert Dec 23 '15 at 12:15
  • 2
    Well, like the error says, that file doesn't seem to be a database. Are you sure it is? A filename ending in .sql is usually a db dump or a series of SQL commands, not an actual queryable database. – Daniel Roseman Dec 23 '15 at 12:16
  • Yes I've just created it in phpmyadmin then export into sql format. How can I change it into SQLite 3 database ???? – Afsanefda Dec 23 '15 at 12:29
  • Yes U R right it's not a SQlite3 DB !!! I'm not a python expert either ! Can I change by any chance ? – Afsanefda Dec 23 '15 at 12:32
  • 3
    See [how to import .sql into sqlite3](http://stackoverflow.com/questions/2049109/how-to-import-sql-into-sqlite3). – agold Dec 23 '15 at 13:32

1 Answers1

2

You have to import your data into SQLite. Since your file is an export of MySQL there might be import problems and you might have to fix some line manually.

But basically you have to create a new SQLite database file and import your SQL. You need the sqlite3 binary for that. Run on the command line:

sqlite3 your_new_db.sqlite
sqlite> .read genres.sql
Klaus D.
  • 13,874
  • 5
  • 41
  • 48