Android数据库错误:int类型的文字2286719584超出范围。?

时间:2012-03-11 22:21:24

标签: android database sqlite

当我输入一个电话号码时,我相信它对于一个数字来说是一个大数字,但是当我把它改成其他任何东西时,它仍然无效。说int类型的文字2286719584超出范围。

包com.mike.Make2;

公共类用户{

//private variables 
int _phone; 
String _fname; 
String _lname; 

// Empty constructor 
public User(){ 

} 
// constructor 
public User(int phoneid, String fname, String lname){ 
    this._phone = phoneid; 
    this._fname = fname; 
    this._lname = lname; 
} 

// constructor 
public User(String fname, String lname){ 
    this._fname = fname; 
    this._lname = lname; 
} 
// getting phone ID 
public int getPhoneID(){ 
    return this._phone; 
} 

// setting phone id 
public void setPhoneID(int phoneid){ 
    this._phone = phoneid; 
} 

// getting first name 
public  String getFirstName(){ 
    return this._fname; 
} 

// setting first name 
public void setFirstName(String fname){ 
    this._fname = fname; 
} 

// getting last name
public String getLastName(){ 
    return this._lname; 
} 

// setting first name 
public void setLastName(String lname){ 
    this._lname = lname; 
} 

}

包com.mike.Make2;

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

public class UserDatabase extends SQLiteOpenHelper { 

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

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

    // User table name 
    private static final String TABLE_USER = "user"; 

    // User Table Columns name 
    private static final String KEY_ID = "phone_number"; 
    private static final String KEY_NAME = "fname"; 
    private static final String KEY_NAME2 = "lname";            


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

    // Creating Tables 
    @Override
    public void onCreate(SQLiteDatabase us) { 
        String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
                + KEY_ID + " INTEGER PRIMARY KEY ," + KEY_NAME + " TEXT,"
                + KEY_NAME2 + " TEXT" + ")"; 
        us.execSQL(CREATE_USER_TABLE); 
    } 

    // Upgrading database 
    @Override
    public void onUpgrade(SQLiteDatabase us, int oldVersion, int newVersion) { 
        // Drop older table if existed 
        us.execSQL("DROP TABLE IF EXISTS " + TABLE_USER); 

        // Create tables again 
        onCreate(us); 
    } 

    // Adding new contact to user 
    void addUser(User user) { 
        SQLiteDatabase us = this.getWritableDatabase(); 

        ContentValues values = new ContentValues(); 
        values.put(KEY_ID, user.getPhoneID()); // contacts phone number
        values.put(KEY_NAME, user.getFirstName()); // Contact first name
        values.put(KEY_NAME2, user.getLastName()); // Contact last name

        // Inserting Row 
        us.insert(TABLE_USER, null, values); 
        us.close(); // Closing database connection 
    } 

    // Getting single contact user
   User getUser(int id) { 
        SQLiteDatabase us = this.getReadableDatabase(); 

        Cursor cursor = us.query(TABLE_USER, new String[] { KEY_ID, 
                KEY_NAME, KEY_NAME2 }, KEY_ID + "=?", 
                new String[] { String.valueOf(id) }, null, null, null, null); 
        if (cursor != null) 
            cursor.moveToFirst(); 

        User user = new User(Integer.parseInt(cursor.getString(0)), 
                cursor.getString(1), cursor.getString(2)); 
        // return user
        return user; 
    } 


    // Updating single contact 
    public int updateUser(User user) { 
        SQLiteDatabase us = this.getWritableDatabase(); 

        ContentValues values = new ContentValues(); 
        values.put(KEY_ID,user.getPhoneID());
        values.put(KEY_NAME, user.getFirstName()); 
        values.put(KEY_NAME2, user.getLastName()); 

        // updating row 
        return us.update(TABLE_USER, values, KEY_ID + " = ?", 
                new String[] { String.valueOf(user.getPhoneID()) }); 
    } 
    public int deleteAll()
    { 
        SQLiteDatabase us = this.getWritableDatabase(); 
        return us.delete(TABLE_USER, null, null);
    }
    public void delete_byPhonenumber(int id)
    { 
        SQLiteDatabase us = this.getWritableDatabase(); 
        us.delete(TABLE_USER, KEY_ID+"="+id, null);
    }
    // Deleting single contact 
    public void deleteUser(User user)
    { 
        SQLiteDatabase us = this.getWritableDatabase(); 

        us.delete(TABLE_USER, KEY_ID + " = ?",   new String[] { String.valueOf(user.getPhoneID()) }); 
        us.close(); 
    } 

    // Getting contacts Count 
    public int getUserCount() { 
        String countQuery = "SELECT  * FROM " + TABLE_USER; 
        SQLiteDatabase us = this.getReadableDatabase(); 
        Cursor cursor = us.rawQuery(countQuery, null); 
        cursor.close(); 

        // return count 
        return cursor.getCount(); 
    } 

}

包com.mike.Make2;

import java.util.List;  
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 


public class SQLiteImplement extends Activity { 
    TextView display,display2,display3;
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.showdb); 


        display = (TextView) findViewById (R.id.tvdb1);

        ContactDatabase db = new ContactDatabase(this); 
        UserDatabase us = new UserDatabase(this);
        // Inserting Contacts 
        Log.d("Insert: ", "Inserting .."); 


        db.addContact(new Contact("Michael", "2286970002")); 
        db.addContact(new Contact("David", "2282160000")); 
        db.addContact(new Contact("Brandon", "2286719584")); 

        us.addUser(new User(2286719584,"Michael","Lynn"));

        // Reading all contacts 
        Log.d("Reading: ", "Reading all contacts.."); 
        List<Contact> contacts = db.getAllContacts();        

        for (Contact cn : contacts)
        { 
            String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
            // need a listAdapter her
            display.setText(log + " last one");
            // Writing Contacts to log 
        Log.d("Name: ", log); 
        }

         //  db.delete_byID(0); starts at 0
         us.delete_byPhonenumber(2286719584);
         //  db.deleteAll(); delete all contacts in the table
         //  us.deleteALL(); delete the main user in the table
    } 
}

1 个答案:

答案 0 :(得分:1)

电话号码应使用字符串(或代表类型)存储。

原因在于,在某些国家/地区(如德国此处),电话号码可能会有前导零。如果使用整数来存储它们,那些前导零将会消失。

此外,由于您不想使用数字进行计算,因此使用VARCHAR(在MySQL中)或TEXT(对于SQLite)似乎是更好的主意。