gpt4 book ai didi

java - SQlite android中的多个表

转载 作者:行者123 更新时间:2023-11-29 05:19:45 25 4
gpt4 key购买 nike

我想在 sqlite android 中创建一个数据库,我想在数据库中创建 2 个表,然后我想动态创建表。但是我的类 DBAdapter 只创建一个表,对于其他表会抛出错误“没有这样的表”,所以我应该怎么做呢?请帮助我

    package com.example.asa;


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 DBAdapter {
public static final String KEY_ROWID_con = "_id";
public static final String KEY_NAME_con = "name";
public static final String KEY_NUMBER_con = "number";
private static final String TAG = "DBAdapter";
private static final String DATABASE_TABLE_con = "contacts";

private static final String DATABASE_NAME = "MyDB";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE_con =
"create table contacts (_id integer primary key autoincrement, "
+ "name text not null, number text not null);";

private final Context context;

private 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)
{
try {
db.execSQL(DATABASE_CREATE_con);
} catch (SQLException e) {
e.printStackTrace();
}
}

@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 chats");
onCreate(db);
}
}

//---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}

//---closes the database---
public void close()
{
DBHelper.close();
}

//---insert a contact into the database---
public long insertContact(String name, String number)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME_con, name);
initialValues.put(KEY_NUMBER_con, number);
return db.insert(DATABASE_TABLE_con, null, initialValues);
}

//---deletes a particular contact---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE_con, KEY_ROWID_con + "=" + rowId, null) > 0;
}

//---retrieves all the contacts---
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE_con, new String[] {KEY_ROWID_con, KEY_NAME_con,
KEY_NUMBER_con}, null, null, null, null, null);
}

//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE_con, new String[] {KEY_ROWID_con,
KEY_NAME_con, KEY_NUMBER_con}, KEY_ROWID_con + "=" + rowId, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}

//---updates a contact---
public boolean updateContact(long rowId, String name, String number)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME_con, name);
args.put(KEY_NUMBER_con, number);
return db.update(DATABASE_TABLE_con, args, KEY_ROWID_con + "=" + rowId, null) > 0;
}

//---Search Contact by String ---//
public String Search(String str)
{
String [] columns = new String[]{ KEY_ROWID_con, KEY_NAME_con, KEY_NUMBER_con};
return db.query(DATABASE_TABLE_con, columns, KEY_NAME_con + "=?", new String[] { str }, null, null, null).getString(0);
}

//---insert a contact into the database---

}

最佳答案

试试这个。我遇到了同样的问题,这个解决了——

package db;

import java.util.ArrayList;

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

public class DB_Handler_exercise extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "exercise_items";
private static final int DATABASE_VERSION = 1;
//table name
private static final String TABLE_EXERCISE_TOP = "exercise_top";
private static final String TABLE_EXERCISE_BOTTOM = "exercise_bottom";
private static final String TABLE_EXERCISE_CARDIO = "exercise_cardio";
private static final String TABLE_EXERCISE_VARIATIONS= "exercise_variations";

//column name
private static final String KEY_EX_ID = "ex_id";
private static final String KEY_EX_NAME = "ex_name";
private static final String KEY_EX_NICKNAME = "ex_nickname";
private static final String KEY_NO_VARIATION = "ex_variation";
private static final String KEY_MAIN_ID = "main_id";
private static final String KEY_VARIATION_ID = "v_id";
private static final String KEY_EX_VARIATION_NAME = "ex_variation_name";
private static final String KEY_EX_VARIATION_STEPS= "ex_variation_steps";
private static final String KEY_EX_VARIATION_PHOTOS = "ex_variation_photos";






public DB_Handler_exercise(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
super.onDowngrade(db, oldVersion, newVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//create table exercise_top
String CREATE_TABLE_TOP="CREATE TABLE IF NOT EXISTS "+TABLE_EXERCISE_TOP + "(" + KEY_EX_ID + " INTEGER PRIMARY KEY," + KEY_EX_NAME + " TEXT," + KEY_EX_NICKNAME + " TEXT," + KEY_NO_VARIATION + " INTEGER" + ")";
db.execSQL(CREATE_TABLE_TOP);
//create table exercise_bottom
String CREATE_TABLE_BOTTOM="CREATE TABLE IF NOT EXISTS " + TABLE_EXERCISE_BOTTOM+ "(" + KEY_EX_ID + " INTEGER PRIMARY KEY," + KEY_EX_NAME + " TEXT," +KEY_EX_NICKNAME + " TEXT," + KEY_NO_VARIATION + " INTEGER" +")";
db.execSQL(CREATE_TABLE_BOTTOM);
//create table exercise_cardio
String CREATE_TABLE_CARDIO="CREATE TABLE IF NOT EXISTS "+ TABLE_EXERCISE_CARDIO+ "(" + KEY_EX_ID + " INTEGER PRIMARY KEY," + KEY_EX_NAME + " TEXT," +KEY_EX_NICKNAME + " TEXT," + KEY_NO_VARIATION + " INTEGER" +")";
db.execSQL(CREATE_TABLE_CARDIO);
//create table exercise_variations
String CREATE_TABLE_VARIATION="CREATE TABLE IF NOT EXISTS "+ TABLE_EXERCISE_VARIATIONS+ "(" + KEY_MAIN_ID + " INTEGER ," + KEY_VARIATION_ID + " INTEGER,"+ KEY_EX_NAME+" TEXT,"+ KEY_EX_NICKNAME+" TEXT," + KEY_EX_VARIATION_NAME + " TEXT," +KEY_EX_VARIATION_STEPS + " TEXT," + KEY_EX_VARIATION_PHOTOS + " TEXT" +")";
db.execSQL(CREATE_TABLE_VARIATION);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}
//adding data on table_top
public void addData_top(Table_exercise_top top){
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_EX_ID, top.getEx_id());
values.put(KEY_EX_NAME, top.getEx_name());
values.put(KEY_EX_NICKNAME, top.getEx_nickname());
values.put(KEY_NO_VARIATION, top.getEx_variation());
db.insert(TABLE_EXERCISE_TOP, null, values);
db.close();
}
//adding data on table_bottom
public void addData_bottom(Table_exercise_bottom bottom){
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_EX_ID, bottom.getEx_id());
values.put(KEY_EX_NAME, bottom.getEx_name());
values.put(KEY_EX_NICKNAME, bottom.getEx_nickname());
values.put(KEY_NO_VARIATION, bottom.getEx_variation());
db.insert(TABLE_EXERCISE_BOTTOM, null, values);
db.close();
}
//adding data on table_cardio
public void addData_cardio(Table_exercise_cardio cardio){
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_EX_ID, cardio.getEx_id());
values.put(KEY_EX_NAME, cardio.getEx_name());
values.put(KEY_EX_NICKNAME, cardio.getEx_nickname());
values.put(KEY_NO_VARIATION, cardio.getEx_variation());
db.insert(TABLE_EXERCISE_CARDIO, null, values);
db.close();
}
//adding data on table_variation
public void addData_variation(Table_variation variation){
SQLiteDatabase db=getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_MAIN_ID, variation.getMain_id());
values.put(KEY_VARIATION_ID, variation.getV_id());
values.put(KEY_EX_NAME, variation.getEx_name());
values.put(KEY_EX_NICKNAME, variation.getEx_nickname());
values.put(KEY_EX_VARIATION_NAME, variation.getVariation_name());
values.put(KEY_EX_VARIATION_STEPS, variation.getVariation_steps());
values.put(KEY_EX_VARIATION_PHOTOS, variation.getVariation_photos());
db.insert(TABLE_EXERCISE_VARIATIONS, null, values);
db.close();
}

}

关于java - SQlite android中的多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25236696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com