在 Android 上的 SQLite 中创建多个表
Posted
技术标签:
【中文标题】在 Android 上的 SQLite 中创建多个表【英文标题】:Creating Multiple tables in SQLite on Android 【发布时间】:2011-07-10 23:04:05 【问题描述】:您好,我想在数据库中创建两个不同的表。这些表是 User_details 和 Creditcard_details 我创建了以下 DBAdapter 类来实现数据库操作,但是当我调用 insertCreditcard() 方法 时,不会插入值。我想知道第二个表是否正在创建。我在某个地方出错了,但不知道我应该在哪里以及应该做什么来纠正这个问题。
我真正想做的是,检查 User_details 表中的 id 字段 与登录活动中提供的用户名和密码,然后分配值将此 id 变量 转换为另一个名为 ccid 的变量,该变量用于在 Creditcard_details 表中搜索行或将值插入其中。
有人可以指导我吗。
package com.androidbook.LoginForm;
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;
import android.widget.Toast;
public class DBAdapter
/*------------------------User Details ---------------------------------*/
public static Cursor d;
public static final String KEY_ROWID = "_id";
public static final String KEY_Name = "name";
public static final String KEY_Username = "username";
public static final String KEY_Password = "password";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "Wallet";
private static final String DATABASE_TABLE = "User_Details";
/*------------------------Credit Cards Table----------------------*/
private static final String KEY_CCID = "_ccid";
private static final String KEY_CCUNAME= "cuname";
private static final String KEY_CCNO = "ccno";
private static final String KEY_CVV = "cvvno";
private static final String EXP_DATE = "expdate";
private static final String CREDITCARDS_TABLE = "Creditcard_Details";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table User_Details (_id integer primary key autoincrement, "
+ "name text not null, username text not null, "
+ "password text not null);";
/*---------------------Create Credit Card Table -------------------------------*/
private static final String CCTABLE_CREATE =
"create table Creditcard_Details ( _ccid integer primary key , "
+ "cuname text not null, ccno text not null, "
+ "cvvno text not null" + "expdate text not null )";//+ "FOREIGN KEY(_ccid) REFERENCES User_Details(_id))";
private final Context context;
public DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
this.context = ctx;
DBHelper = new DatabaseHelper(context);
private static class DatabaseHelper extends SQLiteOpenHelper
DatabaseHelper(Context context)
super(context, DATABASE_NAME, null, DATABASE_VERSION);
@Override
public void onCreate(SQLiteDatabase db)
db.execSQL(DATABASE_CREATE);
db.execSQL(CCTABLE_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 titles");
onCreate(db);
public int Login(String username,String password)
try
Cursor c = null;
c = db.rawQuery("select * from User_Details where username =" + "\""+ username + "\""+" and password="+ "\""+ password + "\"", null);
c.moveToFirst();
String tempid = c.getString(0);
//Toast.makeText(DBAdapter.this, "correct"+" "+c,Toast.LENGTH_LONG).show();
d= c;//CCview(tempid);
return c.getCount();
catch(Exception e)
e.printStackTrace();
return 0;
//-----------------------Display Credit Card -----------------------------
/* public int Getid(String tempid)
Cursor c;
c = db.rawQuery("select id from User_Details where username ="
+ "\""+ username + "\"", null);
return Integer.parseInt(c.getString(0));
*/
public Cursor cursordisplay()
return d;
public Cursor CCview(long menuid)throws SQLException
Cursor mCursor =
db.query(true, CREDITCARDS_TABLE, new String[]
KEY_CCID,
KEY_CCUNAME,
KEY_CCNO,
KEY_CVV,
EXP_DATE,
,
KEY_CCID + "=" + menuid,
null,
null,
null,
null,
null);
if (mCursor != null)
mCursor.moveToFirst();
return mCursor;
//--------------------Entries into Credit Card Table------------------------------------
//---insert a title into the database---
public long insertCreditcard(int i, String j, String k, String l, String date)
ContentValues creditValues = new ContentValues();
creditValues.put(KEY_CCID, i);
creditValues.put(KEY_CCUNAME, j);
creditValues.put(KEY_CCNO, k);
creditValues.put(KEY_CVV, l);
creditValues.put(EXP_DATE, date);
return db.insert(CREDITCARDS_TABLE, null, creditValues);
//---opens the database---
public DBAdapter open() throws SQLException
db = DBHelper.getWritableDatabase();
return this;
//---closes the database---
public void close()
DBHelper.close();
//---insert a title into the database---
public long insertTitle(String name, String username, String password)
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_Name, name);
initialValues.put(KEY_Username, username);
initialValues.put(KEY_Password, password);
return db.insert(DATABASE_TABLE, null, initialValues);
//---deletes a particular title---
public boolean deleteTitle(long rowId)
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
//---retrieves all the titles---
public Cursor getAllTitles()
return db.query(DATABASE_TABLE, new String[]
KEY_ROWID,
KEY_Name,
KEY_Username,
KEY_Password,
null,
null,
null,
null,
null);
//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[]
KEY_ROWID,
KEY_Name,
KEY_Username,
KEY_Password
,
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null)
mCursor.moveToFirst();
return mCursor;
//---updates a title---
public boolean updateTitle(long rowId, String name,
String username, String password)
ContentValues args = new ContentValues();
args.put(KEY_Name, name);
args.put(KEY_Username, username);
args.put(KEY_Password, password);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowId, null) > 0;
【问题讨论】:
你可以有一个单独的表很好,这似乎不是问题。为该表设置单独的函数并调用它们。 @Sheikh Aman 您能否澄清一下,当我从 dbadapter 调用 open() 方法时,是否会打开我创建的两个表? 当您调用 open() 时,它会打开数据库。包含所有表的数据库,因此您可以对数据库保存的所有表运行查询。 你有什么错误吗?? 具体什么时候实例化“DBAdapter”,什么时候调用它的“open”方法? 【参考方案1】:由于我的评分低,我无法发表评论,我肯定可以在评论中回答您的问题:
.getWritableDatabase(); 函数为您提供 - 顾名思义 - 可写数据库。 数据库的所有表部分现在都可以使用了
例如:
public Cursor getStuff(String string)
return db.query(true, TABLE_1, new String[] KEY_COLUMN1, KEY_COLUMN1,
KEY_COLUMN1 , KEY_ID + " = ?", new String[] string ,
null, null, null, null);
所有用大写字母写的都是我班级中定义的字符串常量——因为我很懒。如果您想查询不同的表,只需将 TABLE_1 替换为您想要的表
【讨论】:
【参考方案2】:我猜cvvno text not null
在CCTABLE_CREATE
的创建语句中缺少,
:
private static final String CCTABLE_CREATE =
"create table Creditcard_Details ( _ccid integer primary key , "
+ "cuname text not null, ccno text not null, "
+ "cvvno text not null" + "expdate text not null )";//+ "FOREIGN KEY(_ccid) REFERENCES User_Details(_id))";
这应该是:
private static final String CCTABLE_CREATE =
"create table Creditcard_Details ( _ccid integer primary key , "
+ "cuname text not null, ccno text not null, "
+ "cvvno text not null, " + "expdate text not null )";//+ "FOREIGN KEY(_ccid) REFERENCES User_Details(_id))";
尝试这样做。如果这不起作用,我会进一步挖掘。
【讨论】:
以上是关于在 Android 上的 SQLite 中创建多个表的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 应用程序的 SQLite 中创建排序规则?