1
votes

I have a database in sql format, and I need to make an sqlite copy in order to add it in an Android App I'm making.

So far, I haven't got anything working for me. I tried to import the sql into SQLite Database Browser but it doesn't import properly, it always gives out an error statement. I tried creating a database then import the tables but it crashed.

I tried using sqlite3 via Terminal (I'm using a Mac if that would make any difference), however the .import FILE TABLE format doesn't work, it says: "Error: no such table: TABLE". I tried creating an empty table using "create table test42" then ".import test.sql test42 and variants but it says "Error: near "test": Syntax error".

I'm stuck.

Any help would greatly help. Thanks.

6
From where did you export the database in sql format? SQL isn't 100% compatible across vendors. It would help to see the sql file or examples of commands it uses. - Barum Rho
I exported the database from phpMyAdmin to sql file format - Razgriz
You cannot create a table called "table"... that is a reserved word in SQLite - Barak

6 Answers

0
votes

Importing from MySQL probably does not work because of differences in SQL languages implemented by SQLite and MySQL.

Look through the sql file to see if it uses data types and commands unsupported by SQLite.

0
votes

You might be better off exporting the data as cvs and recreating the table schema in SQLite by hand

0
votes

The file produced by phpMyAdmin probably contains SQL commands to CREATE the tables and INSERT the data. The way to run such a file in SQLite is to use the sqlite3 command interface and use the .read command.

Unfortunately, since the .sql file you created is targeted towards MySQL, it's unlikely that the commands it contains will be compatible with SQLite. You will probably have to edit the .sql file pretty heavily before it can be run by SQLite.

0
votes

Here's some sample code from my app. There is a local DB(SQLite) and web SQL db.

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "android_api";

// Login table name
private static final String TABLE_LOGIN = "login";

// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
public static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
            + KEY_ID + " INTEGER PRIMARY KEY,"
            + KEY_NAME + " TEXT,"
            + KEY_EMAIL + " TEXT UNIQUE,"
            + KEY_UID + " TEXT,"
            + KEY_CREATED_AT + " TEXT" + ")";
    db.execSQL(CREATE_LOGIN_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);

    // Create tables again
    onCreate(db);
}

/**
 * Storing user details in database
 * */
public void addUser(String name, String email, String uid, String created_at) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, name); // Name
    values.put(KEY_EMAIL, email); // Email
    values.put(KEY_UID, uid); // Email
    values.put(KEY_CREATED_AT, created_at); // Created At

    // Inserting Row
    db.insert(TABLE_LOGIN, null, values);
    db.close(); // Closing database connection
}

public void addUser(String email) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_EMAIL, email); // Email

    // Inserting Row
    db.insert(TABLE_LOGIN, null, values);
    db.close(); // Closing database connection
}

public String getUser() {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String user = values.getAsString(KEY_EMAIL);
    Log.v("DBH", user);
    db.close();
    return user;        
}

/**
 * Getting user data from database
 * */
public HashMap<String, String> getUserDetails(){
    HashMap<String,String> user = new HashMap<String,String>();
    String selectQuery = "SELECT  * FROM " + TABLE_LOGIN;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    if(cursor.getCount() > 0){
        user.put("name", cursor.getString(1));
        user.put("email", cursor.getString(2));
        user.put("uid", cursor.getString(3));
        user.put("created_at", cursor.getString(4));
    }
    cursor.close();
    db.close();
    // return user
    return user;
}

/**
 * Getting user login status
 * return true if rows are there in table
 * */
public int getRowCount() {
    String countQuery = "SELECT  * FROM " + TABLE_LOGIN;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    int rowCount = cursor.getCount();
    db.close();
    cursor.close();

    // return row count
    return rowCount;
}

public String returnRows() {
    String response = "";
    String countQuery = "SELECT * FROM " + TABLE_LOGIN;
    SQLiteDatabase db = this.getReadableDatabase();
    for (int j = 0; j < getRowCount(); j++) {
        Cursor cursor = db.rawQuery(countQuery, null);
        response += cursor.getColumnName(j);
    }
    Log.v("LT", response);
    return response;
}

/**
 * Re crate database
 * Delete all tables and create them again
 * */
public void resetTables(){
    SQLiteDatabase db = this.getWritableDatabase();
    // Delete All Rows
    db.delete(TABLE_LOGIN, null, null);
    db.close();
}

}

The addUser method takes data from the web DB and stores it in the local. Here is the code surrounding the addUser method when used:

    try {
        if (json.getString(KEY_SUCCESS) != null) {
            String res = json.getString(KEY_SUCCESS);

            if(Integer.parseInt(res) == 1){
                //user successfully logged in
                // Store user details in SQLite Database
                DatabaseHandler db = new DatabaseHandler(activity.getApplicationContext());
                JSONObject json_user = json.getJSONObject("user");
                //Log.v("name", json_user.getString(KEY_NAME));
                // Clear all previous data in database
                userFunction.logoutUser(activity.getApplicationContext());
                db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), 
                        json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));                        

                responseCode = 1;
                // Close Login Screen
                //finish();

            }else{
                responseCode = 0;
                // Error in login
            }
        }

    } catch (NullPointerException e) {
        e.printStackTrace();

    }
    catch (JSONException e) {
        e.printStackTrace();
    }

    return responseCode;
}
0
votes

What I ended up doing is exporting each table as a CSV, which gave me the table values. All I needed to do then is modify the table headers and I had my sqlite file done. This was a bit time consuming, but did the trick in the end.

0
votes

I have just found one Solution. Firstly You just need to Open your MySql File and Remove extra text.After that In SQliteDb Browser You need to create a Database Which must contain Table or Same column which MySQl file have.After that, Open or Import that(Mysql) file in SQlite DB Browser. It will successfully Convert into Sqlite database. For More Help or Procedure.. See this Tutorial: https://youtu.be/UcAHsm9iwpw Thanks