sqlite数据库不能在我的文件中创建探索android

时间:2015-09-26 03:49:44

标签: android sqlite

你好朋友我通过查询创建sqlite数据库所以我写下面的代码

public class DBHandler extends SQLiteOpenHelper {

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

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

// Contacts table name
private static final String TABLE_CONTACTS = "user";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_EMAIL = "email";
private static final String KEY_NAME = "pin";
private static final String KEY_PH_NO = "key";

public DBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    System.out.println("Call");
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_EMAIL + " TEXT,"+ KEY_NAME + " TEXT,"
            + KEY_PH_NO + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_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_CONTACTS);

    // Create tables again
    onCreate(db);
}
}

Main.java

public class AndroidSQLiteTutorialActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DatabaseHandler db = new DatabaseHandler(this);
      }
    }

当我运行上面的代码时,它不会在我的模拟器的文件浏览选项中创建数据库任何想法我该如何解决?您的所有建议都适用

3 个答案:

答案 0 :(得分:1)

您可以找到您创建的名为

的数据库

/data/data/<Your-Application-Package-Name>/databases/<your-database-name>

使用File explorer将其拉出并重命名为.db3扩展名以在SQLiteExplorer中使用

使用DDMS的File explorer导航到模拟器目录。

答案 1 :(得分:0)

SQLiteOpenHelper在实际需要之前不会创建数据库文件。

致电getWritableDatabase()

答案 2 :(得分:-1)

你应该尝试下面的代码,检查db文件是否退出。 如果没有,那就创建它。

String DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
String DB_NAME = "your_db_name";
public void createDataBase() throws IOException {
    // If database not exists copy it from the assets
    boolean mDataBaseExist = checkDataBase();
    if (!mDataBaseExist) {
        this.getReadableDatabase();
        this.close();
        try {
            // Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } catch (IOException mIOException) {
            throw new Error("ErrorCopyingDataBase");
        }
    }
}

// Check that the database exists here: /data/data/your package/databases/DatabaseName
private boolean checkDataBase() {

    File dbFile = new File(DB_PATH + DB_NAME);
    Log.v("dbFile", dbFile + "   " + dbFile.exists());
    return dbFile.exists();
}                 


 // Copy the database from assets
private void copyDataBase() throws IOException {
    InputStream mInput = mContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;
    OutputStream mOutput = new FileOutputStream(outFileName);
    byte[] mBuffer = new byte[1024];
    int mLength;
    while ((mLength = mInput.read(mBuffer)) > 0) {
        mOutput.write(mBuffer, 0, mLength);
    }
    mOutput.flush();
    mOutput.close();
    mInput.close();
}