如何重命名sqlite数据库名称和表?

时间:2017-03-11 10:32:44

标签: android database sqlite

所以我发现在自定义列表视图中查看了这个数据库信息链接并且它有效,我想修改它。我已经将方法和类名更改为书籍(原来是国家),所有左边是我更改数据库名称和表,但当我尝试重命名数据库名称和表但应用程序不起作用。有人请帮忙。我只是一个业余爱好者所以我有问题提前谢谢你。我已经使用原始代码进行了测试。

这是代码:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class BooksDbAdapter {

public static final String KEY_ROWID = "_id";
public static final String KEY_CODE = "code";
public static final String KEY_NAME = "name";
public static final String KEY_CONTINENT = "continent";


 private static final String TAG = "CountriesDbAdapter";
 private DatabaseHelper mDbHelper;
 private SQLiteDatabase mDb;

 private static final String DATABASE_NAME = "World";
 private static final String SQLITE_TABLE = "Country";
 private static final int DATABASE_VERSION = 1;

 private final Context mCtx;

 private static final String DATABASE_CREATE =
  "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
  KEY_ROWID + " integer PRIMARY KEY autoincrement," +
  KEY_CODE + "," +
  KEY_NAME + "," +
  KEY_CONTINENT + "," +
  " UNIQUE (" + KEY_CODE +"));";

 private static class DatabaseHelper extends SQLiteOpenHelper {

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


  @Override
  public void onCreate(SQLiteDatabase db) {
   Log.w(TAG, DATABASE_CREATE);
   db.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
     + newVersion + ", which will destroy all old data");
   db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
   onCreate(db);
  }
 }

 public BooksDbAdapter(Context ctx) {
  this.mCtx = ctx;
 }

 public BooksDbAdapter open() throws SQLException {
  mDbHelper = new DatabaseHelper(mCtx);
  mDb = mDbHelper.getWritableDatabase();
  return this;
 }

 public void close() {
  if (mDbHelper != null) {
   mDbHelper.close();
  }
 }

 public long createBook(String code, String name, 
   String continent) {

  ContentValues initialValues = new ContentValues();
  initialValues.put(KEY_CODE, code);
  initialValues.put(KEY_NAME, name);
  initialValues.put(KEY_CONTINENT, continent);

  return mDb.insert(SQLITE_TABLE, null, initialValues);
 }

 public boolean deleteAllBooks() {

  int doneDelete = 0;
  doneDelete = mDb.delete(SQLITE_TABLE, null , null);
  Log.w(TAG, Integer.toString(doneDelete));
  return doneDelete > 0;

 }

 public Cursor fetchBooksByName(String inputText) throws SQLException {
  Log.w(TAG, inputText);
  Cursor mCursor = null;
  if (inputText == null  ||  inputText.length () == 0)  {
   mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
     KEY_CODE, KEY_NAME, KEY_CONTINENT}, 
     null, null, null, null, null);

  }
  else {
   mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
     KEY_CODE, KEY_NAME, KEY_CONTINENT}, 
     KEY_NAME + " like '%" + inputText + "%'", null,
     null, null, null, null);
  }
  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;

 }

 public Cursor fetchAllBooks() {

  Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
    KEY_CODE, KEY_NAME, KEY_CONTINENT}, 
    null, null, null, null, null);

  if (mCursor != null) {
   mCursor.moveToFirst();
  }
  return mCursor;
 }

 public void insertSomeBook() {
  createBook("AFG","Afghanistan","Asia");
  createBook("ALB","Albania","Europe");
  createBook("DZA","Algeria","Africa");
  createBook("ASM","American", "Samoa");
  createBook("AND","Andorra","Europe");
  createBook("AGO","Angola","Africa");
  createBook("AIA","Anguilla","North America");

 }

}

1 个答案:

答案 0 :(得分:0)

  

如何重命名sqlite数据库名称和表?

可以在一个应用程序中拥有多个数据库。如果没有多个数据库,则只能有一个包含多个表的数据库。

  

如何创建多个表

     private static final String TABLE1 =
          "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
          KEY_ROWID + " integer PRIMARY KEY autoincrement," +
          KEY_CODE + "," +
          KEY_NAME + "," +
          KEY_CONTINENT + "," +
          " UNIQUE (" + KEY_CODE +"));";   

private static final String TABLE2 =
          "CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
          KEY_ROWID + " integer PRIMARY KEY autoincrement," +
          KEY_CODE + "," +
          KEY_NAME + "," +
          KEY_CONTINENT + "," +
          " UNIQUE (" + KEY_CODE +"));";  

            @Override
              public void onCreate(SQLiteDatabase db) {    
               db.execSQL(TABLE1);
               db.execSQL(TABLE2);
              }