在Android中创建表格 [英] Creating table in Android

查看:92
本文介绍了在Android中创建表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI

任何人都可以帮助我,我得到一个错误提示....[sqlite返回:错误代码= 1,msg =没有这样的表:CustomerDetails] ...但是我的表在那里

这是我的下面的代码...请帮助


-------------------------------------------------- ----------------------------------

包com.sdc;


HI

Anyone help me please and i''m getting an error says....[sqlite returned: error code = 1, msg = no such table: CustomerDetails]...but my table is there

here is my code below...please help


------------------------------------------------------------------------------------

package com.sdc;


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.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 
//import android.util.Log;
import android.util.Log;


public class DBAdapter {
	
	// Constants
		
	public static final String COLUMN_ID = "ID";
	public static final String COLUMN_NAME = "NAME";
	public static final String COLUMN_SURNAME = "SURNAME";
	public static final String COLUMN_DATE_OF_BIRTH = "DateOfBirth";
	public static final String COLUMN_ADDRESS = "HomeAddress";
	public static final String COLUMN_EMAIL = "EmailNO";
	public static final String COLUMN_PHONE_NUMBER = "PhoneNumber";
	public static final String COLUMN_CITY = "City";
	public static final String COLUMN_PTYE_PAYMENT = "TypePayment";
	public static final String COLUMN_SHIPPING_TYPE = "ShippingType";
	public static final String COLUMN_CARD_NUMBER = "CardNumber";
	public static final String TAG = "DBAdapter";
	
	private static final String DATABASE_NAME = "Super_Computers.db";
	private static final int DATABASE_VERSION = 3;
	private static final String DATABASE_TABLE = "CustomerDetails";
	
	
	
	private static final String DATABASE_CREATE =
		"create table  CustomerDetails (id integer primary key autoincrement, "
			+ " NAME VARCHAR not null, SURNAME VARCHAR not null,DateOfBirth date not null," +
			"   HomeAddress VARCHAR not null,EmailNO VARCHAR not null,PhoneNumber number , City VARCHAR not null," +
			"TypePayment VARCHAR not null,ShippingType VARCHAR not null, CardNumber number);";
				
	
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);
	} 
	   catch (SQLException e) {
		   e.printStackTrace();
		
	   }
	}
@Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion , int newVesion) {

	Log.w(TAG, "Upgrading database from version " + oldVersion + "to"
			+ newVesion + ", which will destroy all old data");
	db.execSQL("DROP TABLE IF EXIST CustomerDetails ");
	

   } 
 }

推荐答案

更改SQLiteOpenHelper的onUpgrade,如下所示


Change the onUpgrade of SQLiteOpenHelper like below


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion , int newVesion) {

  Log.w(TAG, "Upgrading database from version " + oldVersion + "to"
          + newVesion + ", which will destroy all old data");
  db.execSQL("DROP TABLE IF EXIST CustomerDetails ");
  onCreate(db);

}



可能是您将版本从1更改为3,然后根据您的代码,onUpgrade方法仅删除了表,它不会创建新更新的表,所以我添加了



May be you change the version from 1 to 3 then as per your code the onUpgrade method only drop the table, It will not create the newly updated table so i added

onCreate(db);




在删除表格后在onUpgrade中




in onUpgrade after Drop the table


这篇关于在Android中创建表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆