1

我有一个 sql 格式的数据库,我需要制作一个 sqlite 副本才能将其添加到我正在制作的 Android 应用程序中。

到目前为止,我还没有任何对我有用的东西。我试图将 sql 导入 SQLite 数据库浏览器,但它没有正确导入,它总是给出一个错误语句。我尝试创建一个数据库然后导入表,但它崩溃了。

我尝试通过终端使用 sqlite3(如果有任何区别,我使用的是 Mac),但是 .import FILE TABLE 格式不起作用,它说:“错误:没有这样的表:TABLE”。我尝试使用“create table test42”然后“.import test.sql test42 and variables”创建一个空表,但它显示“错误:靠近“测试”:语法错误”。

我被困住了。

任何帮助都会有很大帮助。谢谢。

4

6 回答 6

0

这是我的应用程序中的一些示例代码。有一个本地数据库(SQLite)和网络 SQL 数据库。

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();
}

}

addUser 方法从 Web 数据库中获取数据并将其存储在本地。以下是使用时围绕 addUser 方法的代码:

    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;
}
于 2012-05-08T04:06:59.453 回答
0

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.

于 2012-05-08T03:46:31.413 回答
0

由于 SQLite 和 MySQL 实现的 SQL 语言不同,从 MySQL 导入可能不起作用。

查看 sql 文件,看看它是否使用了 SQLite 不支持的数据类型命令。

于 2012-05-08T02:24:50.073 回答
0

您最好将数据导出为 cvs 并手动在 SQLite 中重新创建表模式

于 2012-05-08T02:34:02.507 回答
0

我最终做的是将每个表导出为 CSV,这给了我表值。然后我需要做的就是修改表头,然后我完成了我的 sqlite 文件。这有点耗时,但最终成功了。

于 2012-11-07T22:45:17.493 回答
0

我刚刚找到了一个解决方案。首先,您只需要打开 MySql 文件并删除多余的文本。之后,在 SQliteDb 浏览器中,您需要创建一个数据库,该数据库必须包含 MySQl 文件所具有的表或相同列。之后,在 SQlite 中打开或导入该(Mysql)文件数据库浏览器。它将成功转换为 Sqlite 数据库。如需更多帮助或程序。请参阅本教程: https ://youtu.be/UcAHsm9iwpw 谢谢

于 2017-06-03T18:47:12.047 回答